.open()
Anonymous contributor
Published Apr 1, 2025
Contribute to Docs
In Pillow, the .open()
function opens an image file as an Image
object. This is the first step in manipulating an image. However, it does not automatically read or display the image. To view or process the image, methods like .show()
(to display) or .load()
(to force loading into memory) can be used.
Syntax
Image.open(fp, mode='r')
Parameters:
fp
: The file path or a file-like object.mode
: The mode to open the file.'r'
(read) is the default and the only supported mode.
Return value:
An Image
object that represents the opened image.
Example
In this example, the .open()
function loads an image as an Image
object, and using with
ensures automatic closure of the file after processing:
# Import the Image module from the PIL libraryfrom PIL import Image# Open an image file and assign it to a variableimage = Image.open('file-path')# Alternatively, use `with` to ensure the file is properly closed after processingwith Image.open('file-path') as image:# Perform image processing herepass
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:Pillow on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours