Python Pillow read and save images

Reading and Saving Images with Python Pillow

This chapter covers topics such as how to read and save images in Pillow.

Reading Images

Reading and writing images using the Pillow library is straightforward with the help of the PIL.Image module functions.

Syntax

Image.open(fp, mode='r')

Where

  • fp – A filename (string), a pathlib.Path object, or a file object. The file object must implement the read(), seek(), and tell() methods and be opened in binary mode.
  • mode – This is an optional parameter that, if given, must be ‘r’.

  • Return Value – An image object.

  • Error – If the file cannot be found, or the image cannot be opened and recognized.

Example

Here is a very simple example where we will open an image in any format (we’ll use .jpg), display it in a window, and then save it to another file format (.png) (to the default location).

from PIL import Image
image = Image.open('beach1.jpg')
image.show()
image.save('beach1.bmp')
image1 = Image.open('beach1.bmp')
image1.show()

In the example above, we import the Image module from the PIL library and then call the Image.open() function to read an image from disk. It returns an image object data type. It automatically determines the file type by looking at the file contents. For reading, the open() function accepts a filename (string), a path object, or an image (file) object.

So, by using the open() function, we are actually reading an image. Image.open() will read the image and retrieve all relevant information from it.

Output

If you save the above program as Example.py and execute it, it will display the original (.jpg) and the resaved (.bmp) image using standard PNG display tools, as shown below.

Actual Image

Python Pillow - Working with Images

Resaved Image (.bmp)

Python Pillow - Working with Images

Saving an Image

The save() function writes an image to a file. Like reading (open()), save() accepts a filename, a path object, or an already opened file object for writing.

Syntax

Image.save(fp, format=None, **params)

Where:

  • fp – A filename (string), pathlib.Path object, or file object.
  • format – An optional format override. If omitted, the format used is determined by the filename extension. This parameter should always be used if a file object is used instead of a filename.

  • options – Additional parameters for the image writer.

  • Return value – None

  • KeyError – If the output format cannot be determined from the filename, use the format options to resolve the issue.

  • IOError – If the file could not be written, the file may have already been created and may contain partial data.

In short, the above syntax will save the image under the given filename. If no format is specified, the current filename extension is used. To provide additional instructions to the writer, keyword options are used.

image.save('beach1.bmp')

In the example above, the image type is determined based on the file extension. For example, this will create a bmp file in our current working directory.

You can also explicitly specify the file type as the second argument –

image.save('beach1.gif', 'GIF')

Leave a Reply

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