ImageFile module in Pillow

Pillow is a successor project of Python’s original image processing module, the Python Imaging Library (PIL), which is in discontinuation since 2011. It provides various functionalities for working on digital images in Python. Apart from basic image operations like open, display, rotate, save etc, you can use the Pillow module for filtering images and changing color spaces too. The current version of Pillow accepts various formats, such as PNG, JPEG, GIF, PPM, TIFF and BMP.
In this tutorial, you will learn in particular about the ImageFile module of Pillow in Python.

Installing Pillow

To use the ImageFile, first of all, you need to install the Pillow module. You can do the same by running the below command on your terminal.

pip install pillow

Once you have the Pillow module installed, you can proceed to explore it further.

ImageFile module: Support for open() and save()

  • Saving an image
    from PIL import Image
    import PIL
    image ="saved.jpg")

    The above code will save the image file from the specified path and save it with the specified name.

  • Opening an image
    from PIL import Image
    image ="C:\Users\varsh\Desktop\images1.jpeg")

    The above code opens the image file whose path is specified in any available image viewer.

ImageFile module: Support for decoding an image

A Parser class is present in the ImageFile module and can be used to decode(parse) a given image, piece by piece. The code for the said operation:

from PIL import ImageFile
fp = open("images1.jpeg", "rb")
p = ImageFile.Parser()
while 1:
    s =
    if not s:
im = p.close()"parsed_copy.jpg")

You can store the parsed copy of the image using the save() as shown.

Some other modules provided by Pillow for Image Processing include ImageMath, ImageMorph, ImageFilter etc.
Each of these Pillow modules provides you with various image processing options. You should try exploring them too!

2. show(): The show() method displays the image to the external viewer.

from PIL import Image

img ='pic1.jpg')

3. rotate(): The rotate() method is used to rotate the image. The following code rotates the image to 90 degrees and displays it to the external viewer.

from PIL import Image

img ='pic1.jpg')

4. resize(): The resize() method is used to resize the original image. It takes a tuple as a parameter. The tuple consists of width and height.

from PIL import Image

img ='pic1.jpeg')
img.resize((500, 500))

This code will resize the image which will have dimensions of 500×500.

5. crop(): This method is used to crop the image. This method takes a 4 tuple input. The tuple consists of 4-pixel co-ordinate which are left, upper, right, lower.

from PIL import Image

img ='pic1.jpg')
imgCrop = img.crop((20, 20, 100, 100))

6. save(): The save() method is used to save the image. When we make some changes to an image and to save those changes we use save() method.

from PIL import Image

img ='pic1.jpg')

Here I have rotated the image at 45 degrees and resized it to 450×500 pixels and the saved it as pic5.jpg. Here I am saving the image in the same directory but if you wish to save the image in some other directory then specify the path to that directory.


1. filename: Returns the filename or the directory of the file. Only images created with open() function have filename attribute.

2. format: Returns the format of the file.

3. mode: Mode attribute returns the pixel format used by the images. The values that can be returned are ‘1’, ‘L’, ‘RGB’ or ‘CMYK’.

4. size: Returns the image size in pixel. It returns a 2-tuple which has width and height.

5. width: Returns the width of the image in pixels.

6. height: Returns the height of the image in pixels.

from PIL import Image

img ='pic1.jpg')
print("Filename: ",img.filename)
print("Format: ",img.format)
print("Mode: ",img.mode)
print("Size: ",img.size)
print("Width: ",img.width)
print("Height: ",img.height)


Filename: pic1.jpg
Format: JPEG
Mode: RGB
Size: (1418, 741)
Width: 1418
Height: 741

These are some of the most commonly used methods and attributes of the Image class.

Thank you guys for staying around till the end. We will meet in the next tutorial where I will be covering ImageColor class from the pillow library.

Also read, Get the Basic image information with Pillow | Python

Leave a Reply

Your email address will not be published. Required fields are marked *