Skip to content

How to open, show and save images in PIL (pillow) – with Python Examples

Python Tutorials

Introduction

Python PIL (pillow) library can be used for advanced image processing needs but you will still need to cover the basics about handling images.

In this Python tutorial, we’re going to show you how to open, show and save an image using PIL (pillow) library in Python.

To work with images in PIL you need to first import the Image module from the PIL library in Python.

from PIL import Image

.open() method from PIL’s Image module

To open an image in PIL .open() method can be used.

If you encounter an error with the path to your image because there can be operating system conflicts especially regarding directory representation.

In most cases it helps to type the file path in raw string format or use backslashes instead of regular slashes.

At this point your image will be assigned to img variable but nothing will happen until you use the .show method as below.

.show() method from PIL’s Image module

Once you opened an image you can show it using the .show method.

.save() method from PIL’s Image module

After you worked on an image, do some image processing or image manipulation you will likely want to save this new version of your image.

Saving an image can be achieved by using .save() method with PIL library’s Image module in Python.

Example 1: How to open an image with PIL

To open an image in PIL you need to first import the Image module from the PIL library in Python.

from PIL import Image

file = "C://Users/ABC/Motorbike.jpg"
img = Image.open(file)

Example 2: How to show an image with PIL

Here is a code example:

from PIL import Image

file = "C://Users/ABC/Motorbike.jpg"
img = Image.open(file)

img.show()

Example 3: How to save an image with PIL

Here is a code example:

from PIL import Image

file = "C://Users/ABC/Motorbike.jpg"
img = Image.open(file)

img.save(Desktop/new_img.jpg)

Tip 1: PIL is smart about opening images

PIL imaging library is pretty smart. Although it might not be ideal in all cases you can open files without even typing their full name or file extensions.

PIL library’s Image module will forgive such mistakes while opening files and figure out the file extension of the image by itself.

Here is a code example:

from PIL import Image

file = "C://Users/ABC/Motorbike"
img = Image.open(file)

Tip 2: Incorporate try / except statements in your code

It’s common to work with multiple images during image processing as it can be very convenient.

When you have a loop or algorithm to go through hundreds or thousands of images, mistakes can happen.

If you use try / except statements your code will run uninterrupted despite individual errors.

You can read more about the correct usage on our try / except statements Python tutorial and common Python Errors tutorial.

Here is a code example:

from PIL import Image

folder_path = "C://Users/ABC/Images123"
for i in folder_path:
    try:
        img = Image.open(i)
    except Exception:
        pass

Resources

If you’d like to see more interesting content regarding image processing in Python you may want to check out our more advanced Python PIL tutorials as well as Digital Imaging basics below:

Summary

In this Python Tutorial, we’ve seen how to open, show and save images in Python using PIL library’s Image module.

We have demonstrated these topics with Python code examples and we have also shared a couple of useful and interesting Python tips that can help you become a more advanced Python coder.