Python PIL | Image.open() method
PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The
Image
module provides a class with the same name which is used to represent a PIL image. The module also provides a number of factory functions, including functions to load images from files, and to create new images.
PIL.Image.open()
Opens and identifies the given image file. This is a lazy operation; this function identifies the file, but the file remains open and the actual image data is not read from the file until you try to process the data (or call the load() method). See new().
Syntax: PIL.Image.open(fp, mode=’r’)
Parameters: fp – A filename (string), pathlib.Path object or a file object. The file object must implement read(), seek(), and tell() methods, and be opened in binary mode.
mode – The mode. If given, this argument must be “r”.
Returns type: An image object.
Raises: IOError – If the file cannot be found, or the image cannot be opened and identified.
Image Used:

# Imports PIL module
from PIL import Image
# open method used to open different extension image file
im = Image.open(r"C:\Users\System-Pc\Desktop\ybear.jpg")
# This method will show image in any image viewer
im.show()
Output:
.JPG extension image open.

Another Example:
Here we used .PNG extension file.
Image Used:

# Imports PIL module
from PIL import Image
# open method used to open different extension image file
im = Image.open(r"C:\Users\System-Pc\Desktop\lion.png")
# This method will show image in any image viewer
im.show()
Output:
.PNG extension image open.

Python PIL | Image.open() method – FAQs
How to Import Image in Python Using PIL
To import (or load) an image in Python using the PIL library, which is now known as Pillow, you first need to ensure Pillow is installed and then use the
Imagemodule to open an image file. Here’s how to do it:from PIL import Image
# Load an image
image = Image.open('path_to_image.jpg')
image.show() # This will display the image using a default viewer
How to Save Images Using PIL
To save an image using PIL (Pillow), you can use the
save()method of theImageobject. This method allows you to specify the filename and format for the image to be saved:from PIL import Image
# Load an image
image = Image.open('path_to_image.jpg')
# Save the image with a new filename or format
image.save('path_to_new_image.png', 'PNG')
What Does PIL Stand For in Python?
PIL stands for Python Imaging Library. It was the original library designed for image processing in Python. The project was discontinued and later forked to what is now known as Pillow, which is maintained and provides the same functionality with additional features.
How to Install PIL in Python Command
To install PIL, you actually install Pillow, which is the active fork of PIL and supports all modern Python versions. You can install it using pip, the Python package installer:
pip install PillowThis command installs the Pillow library, which includes all the functionality originally available in PIL plus additional enhancements.
How to Convert PNG to JPG with PIL
Converting an image from PNG to JPG involves loading the PNG file and then saving it in JPG format. Here’s how to do it with Pillow:
from PIL import Image
# Load a PNG image
image = Image.open('path_to_image.png')
# Convert the image to 'RGB' mode if it's not (JPG does not support transparency)
if image.mode != 'RGB':
image = image.convert('RGB')
# Save the image as a JPG
image.save('output_image.jpg', 'JPEG')This process not only changes the file format but also ensures compatibility by converting the image mode to RGB, necessary because the JPEG format does not support transparency like PNG.
Enhance your coding skills with DSA Python, a comprehensive course focused on Data Structures and Algorithms using Python. Over 90 days, you'll explore essential algorithms, learn how to solve complex problems, and sharpen your Python programming skills. This course is perfect for anyone looking to level up their coding abilities and get ready for top tech interviews.
Join the Three 90 Challenge and commit to completing 90% of the course in 90 days to earn a 90% refund. It’s the perfect way to stay focused, track your progress, and reap the rewards of your hard work. Take the challenge and become a DSA expert in Python today


