Python PIL | Image.open() method (original) (raw)

Last Updated : 7 Oct, 2025

**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:

**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: