![]() |
VOOZH | about |
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
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.
Image.open() is used to load an image file as an Image object for viewing or further processing.
Output
This example saves the image as a PNG file regardless of its original format.
This code will create a new file named new_image.png in the same directory as the script.
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.
The code creates a new file named output.jpg in the same directory.
No output is displayed in the terminal.
Explanation: