Open In App

Python PIL | Image.open() method

Last Updated : 07 Oct, 2025
Comments
Improve
Suggest changes
23 Likes
Like
Report

Python Imaging Library (PIL), maintained as Pillow, is a popular library for image processing in Python. Its Image.open() method is used to open an image file and return it as an Image object, which can then be displayed, edited, or saved in a different format.

To begin using Pillow, it must first be installed through the Python package manager:

pip install Pillow

Syntax

Image.open(fp, mode="r")

Parameters:

  • fp: Path, file object, or Pathlib.Path.
  • mode: Must be "r" (read mode).

Returns: An Image object and Raises IOError if the file cannot be opened.

Note: This is a lazy operation. The image is identified but not fully read until you access its data.

Example 1: Opening an Image

Image.open() is used to load an image file as an Image object for viewing or further processing.

Python
from PIL import Image
img = Image.open("example.png")
img.show()  

Output

gfg
geeks for geeks

Example 2: Saving an image

This example saves the image as a PNG file regardless of its original format.

Python
from PIL import Image
img = Image.open("example.jpg")
# Save it in PNG format
img.save("new_image.png")


This code will create a new file named new_image.png in the same directory as the script.

Example 3: Converting Image Formats

When converting between formats, it is important to consider compatibility. For example, the JPEG format does not support transparency. Thus, when converting from PNG to JPEG, the image should first be converted to RGB mode.

Python
img = Image.open("example.png")
# Convert to RGB mode if necessary
if img.mode ! = "RGB":
    img = img.convert("RGB")
img.save("output.jpg", "JPEG")

The code creates a new file named output.jpg in the same directory.
No output is displayed in the terminal.

Explanation:

  • Image.open("example.png"): loads the PNG file.
  • img.convert("RGB"): converts the image to RGB if it is not already in RGB mode.
  • img.save(): saves the converted image as "output.jpg".

Article Tags :

Explore