Open an Image using PIL in Python

thumbnail

In this tutorial, you will learn to open an image using PIL in Python. PIL is the python imaging library that provides interpreter from python with image editing function. The Image module provides a class with the same name which is used to represent PIL image that includes functions to load image from files and create new images.

PIL.image.open() is a function to opens and indentifies the given image file. The syntax is explained  as follows.

Syntax: PIL.Image.open(filename, mode=’r’)

Parameters:

filename: A filename in string format, 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 in PIL. If given, this argument must be ‘r’.

Returns Type: An image object.

Raises: IOError – if file cannot be found, or the image cannot be opened and identified.

The image I used:

lena.bmp
lena.bmp

The source code is show below.

# Created by inpows
# Visit inpows.com for more python tutorials
 
# Imports PIL module  
from PIL import Image 
  
# open method used to open different extension image file 
im = Image.open(r"D:\MyProject\Python\ImageDownloader\lena.bmp")  
  
# This method will show image in any image viewer  
im.show()

The output is show as follow.

lena.bmp
lena.bmp

Read More:

Back To Top