Python Pillow - Using Image Module (2024)

To display the image, pillow library is using an image class within it. The image module inside pillow package contains some important inbuilt functions like, load images or create new images, etc.

Opening, rotating and displaying an image

To load the image, we simply import the image module from the pillow and call the Image.open(), passing the image filename.

Instead of calling the Pillow module, we will call the PIL module as to make it backward compatible with an older module called Python Imaging Library (PIL). That’s why our code starts with “from PIL import Image” instead of “from Pillow import Image”.

Next, we’re going to load the image by calling the Image.open() function, which returns a value of the Image object data type. Any modification we make to the image object can be saved to an image file with the save() method. The image object we received using Image.open(), later can be used to resize, crop, draw or other image manipulation method calls on this Image object.

Example

Following example demonstrates the rotation of an image using python pillow −

from PIL import Image#Open image using Image moduleim = Image.open("images/cuba.jpg")#Show actual Imageim.show()#Show rotated Imageim = im.rotate(45)im.show()

Output

If you save the above program as Example.py and execute, it displays the original and rotated images using standard PNG display utility, as follows −

Actual image

Python Pillow - Using Image Module (1)

Rotated image (45 degrees)

Python Pillow - Using Image Module (2)

Attributes of Image Module

The instance of the Image class has some attributes. Let’s try to understand few of them by example −

Image.filename

This function is used to get the file name or the path of the image.

>>>image = Image.open('beach1.jpg')>>> image.filename'beach1.jpg'

Image.format

This function returns file format of the image file like ‘JPEG’, ‘BMP’, ‘PNG’, etc.

>>> image = Image.open('beach1.jpg')>>>>>> image.format'JPEG'

Image.mode

It is used to get the pixel format used by the image. Typical values are “1”, “L”, “RGB” or “CMYK”.

>>> image.mode'RGB'

Image.size

It returns the tuple consist of height & weight of the image.

>>> image.size(1280, 721)

Image.width

It returns only the width of the image.

>>> image.width1280

Image.height

It returns only the height of the image.

>>> image.height721

Image.info

It returns a dictionary holding data associated with the image.

>>> image.info{'jfif': 257, 'jfif_version': (1, 1), 'dpi': (300, 300), 'jfif_unit': 1, 'jfif_density': (300, 300), 'exif': b"Exif\x00\x00MM\x00*\x00\x00\x00........\xeb\x00\x00'\x10\x00\x00\xd7\xb3\x00\x00\x03\xe8"}

Image.palette

It returns the colour palette table, if any.

>>> image.palette

Output above − None

Advertisem*nts

Python Pillow - Using Image Module (2024)
Top Articles
Relative Strength Index (RSI) Indicator: Meanig & Importance | Finschool
PBS Online: Only A Teacher: Schoolhouse Pioneers
The Tribes and Castes of the Central Provinces of India, Volume 3
Nullreferenceexception 7 Days To Die
Dunhams Treestands
Golden Abyss - Chapter 5 - Lunar_Angel
Genesis Parsippany
Brendon Tyler Wharton Height
Craigslist Pet Phoenix
Optimal Perks Rs3
10000 Divided By 5
Meg 2: The Trench Showtimes Near Phoenix Theatres Laurel Park
270 West Michigan residents receive expert driver’s license restoration advice at last major Road to Restoration Clinic of the year
Corporate Homepage | Publix Super Markets
Www.paystubportal.com/7-11 Login
Which Is A Popular Southern Hemisphere Destination Microsoft Rewards
R Tiktoksweets
Syracuse Jr High Home Page
Aspen.sprout Forum
Ge-Tracker Bond
Lakers Game Summary
north jersey garage & moving sales - craigslist
Lost Pizza Nutrition
Living Shard Calamity
Delectable Birthday Dyes
1145 Barnett Drive
From This Corner - Chief Glen Brock: A Shawnee Thinker
Lacey Costco Gas Price
Frequently Asked Questions - Hy-Vee PERKS
Productos para el Cuidado del Cabello Después de un Alisado: Tips y Consejos
Grays Anatomy Wiki
Solarmovie Ma
Human Unitec International Inc (HMNU) Stock Price History Chart & Technical Analysis Graph - TipRanks.com
24 slang words teens and Gen Zers are using in 2020, and what they really mean
Craigslist Com Humboldt
Navigating change - the workplace of tomorrow - key takeaways
Glossytightsglamour
What Time Is First Light Tomorrow Morning
Watchseries To New Domain
Craigs List Hartford
Below Five Store Near Me
Mbfs Com Login
Grizzly Expiration Date Chart 2023
Gabrielle Abbate Obituary
Zom 100 Mbti
A jovem que batizou lei após ser sequestrada por 'amigo virtual'
Walmart Front Door Wreaths
Cryptoquote Solver For Today
Electric Toothbrush Feature Crossword
Free Carnival-themed Google Slides & PowerPoint templates
Mazda 3 Depreciation
Latest Posts
Article information

Author: Eusebia Nader

Last Updated:

Views: 5919

Rating: 5 / 5 (80 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Eusebia Nader

Birthday: 1994-11-11

Address: Apt. 721 977 Ebert Meadows, Jereville, GA 73618-6603

Phone: +2316203969400

Job: International Farming Consultant

Hobby: Reading, Photography, Shooting, Singing, Magic, Kayaking, Mushroom hunting

Introduction: My name is Eusebia Nader, I am a encouraging, brainy, lively, nice, famous, healthy, clever person who loves writing and wants to share my knowledge and understanding with you.