+

Python PIL | Image.open () method

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 the PIL module

from PIL import Image

  
# the open method is used to open a file with a different extension

im = Image. open (r "C: UsersSystem-PcDesktopybear.jpg"

 
# This method will show the image in any viewer images
im.show () 

Output: .JPG image extension open.

Another example: here we used the .PNG file extension.

Image used:

  

 
# Imports the module PIL

from PIL import Image

 
# open method is used to open a file with a different extension

im = Image. open (r "C: UsersSystem-PcDesktoplion.png "

  
# This method will show the image in any image viewer
im.show () 

Output: .PNG image extension is open. 

Get Solution for free from DataCamp guru