fromarray()
Published Jun 8, 2024
Contribute to Docs
.fromarray()
is a function from the Image module that is used to process images for more clarity. It creates a new image derived from a specified array.
Syntax
Image.fromarray(obj_array, mode="CMYK")
.fromarray()
takes two parameters:
obj_array
: It represents a defined arraymode
: It is an optional parameter that defines the type and depth of a pixel in the image that will be created. Ifmode
is not used, the new image will be stored as is.
The mode
parameter can take the following values:
CMYK
: It represents the color palette made from base colors Cyan, Magenta, Yellow, and Key(black). Each color has an 8-bit pixel to store its value, which meansCMYK
has 4x8-bit pixels to store the values.RGB
: It represents the color palette made from Red, Green, and Blue colors. It has 3x8-bit pixels available to store the values.L
: It represents grayscale or shade of gray from white to black. It only has an 8-bit pixel to store its value.
Example
The example below shows the value of a pixel in coordinate (0,0) after the parameter mode
is used with CMYK
as the value.
# Import Image module from Pillow libraryfrom PIL import Imageimport numpy as np# Defined an array 300 by 36 with 200 as all the pixel valuesobj_array = np.full((300, 36), 200)# Create an image from the array, as our `obj_array' is stored in the form of a# numbered arraynew_imageA = Image.fromarray(obj_array, mode="CMYK")# Show the image generated using .fromarray()new_imageA.show()# Show the pixel value by the coordinateprint(new_imageA.getpixel((0,0)))
The result proves that the obj_array has been successfully generated as new_imageA, and the pixel at coordinate (0,0) is stored as a CMYK color combination with the values shown below.
(200, 0, 0, 0)
Codebyte Example
Run the following Codebyte example to understand how a new image is created from the specified array in the RGB
color format.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:Pillow on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Skill path
Data Science Foundations
Learn to clean, analyze, and visualize data with Python and SQL.Includes 15 CoursesWith CertificateBeginner Friendly54 hours - Career path
Data Scientist: Machine Learning Specialist
Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.Includes 27 CoursesWith Professional CertificationBeginner Friendly90 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours