How to Display and Convert Images in Python
Python continues to grow by the day. This introductory tutorial walks you through the process to load, display, and convert images in Python.
Join the DZone community and get the full member experience.
Join For FreePython is gaining more attention and attraction than most other programming languages today. It's gained that popularity for a variety of reasons. It is both object-oriented and procedural, it's open-sourced and extensible, it's portable, and it has library support. Most importantly, however, is that Python is simple to code and very readable.
To illustrate how easy some things are to do in Python, we'll take a quick look at working with images. We’ll show how to load and display an image using Python, how to get image information using Python, and how to convert image formats using Python.
Crash Course on Python | Enroll for Free Today*
*Affiliate link. See Terms of Use.
Of course, to use images in Python, you need to have Python installed. Additionally, you will want to make sure you have the Pillow
module installed. Installing the Pillow
module can be done using the simple command line directive of:
pip install pillow
This will install an updated version of the Python Image Library (PIL), which will allow you to load and use images. Once you've installed Pillow
, you are ready to start working with images.
Loading and Displaying an Image in Python
Writing an application to display an image in Python can be done with just a couple of lines of code as shown in Listing 1:
Listing 1: Img.py: Loading an Image in Python
from PIL import Image
image = Image.open('image.jpg')
image.show()
The code in Listing 1
starts by importing the Image
library. With the library loaded, you can load an image by call in the Image.open()
method and assign the return value to a variable. With the image loaded, you are ready to display the image by calling the show()
method on your image variable. If you run Listing 1
in a directory that contains an image named image.jpg, the image will be displayed. The following shows the image being displayed on a Windows system:
Showing Image Properties Using Python
In addition to showing an image, the Image
class provides a number of other properties and methods that can be used with images. Some of the key properties include:
filename
: The name of the image fileformat
: The file format such as JPG, GIFmode
: The image mode such as RGB, RFBA, CMYK, or YCbCrsize
: The image size in pixels displayed as a width, height tuplewidth
: The width of the image in pixelsheight
: The height of the image in pixelsis_animated
: Indicates if the image is more than one frame.True
if it is,False
if it is not. This attribute is not included in all images.
Listing 2
shows how simple it is to load an image called ani.gif
and display a bunch of its attributes. The image is included in this article just before the code:
Listing 2: Img.py
: Displaying Image Attributes Using Python
from PIL import Image
image = Image.open('ani.gif')
print("Filename: ", image.filename)
print("Format: ", image.format)
print("Mode: ", image.mode)
print("Size: ", image.size)
print("Width: ", image.width)
print("Height: ", image.height)
print("Is Animated: ", (getattr(image, "is_animated", False)))
image.close() # close image file
It is worth noting that the ani.gif
image is an animated gif, so when this listing is executed, the following information is displayed:
Filename: ani.gif
Format: GIF
Mode: P
('Size: ', (500, 500))
('Width: ', 500)
('Height: ', 500)
('Is Animated: ', True)
You can see that the animated gif is 500 by 500 pixels, it is indeed a GIF, and it is animated. In addition to the properties shown in Listing 2
, there are two others you can check.
The first of these properties is palette
. This is the color palette table if one is included with the image. The value is None
if there is not a palette. For the ani.gif
shown above, the color palette information that is shown when calling the property is:
<PIL.ImagePalette.ImagePalette object at 0x000000000358CB88>
For the original JPG image used in Listing 1
, the palette information was None
.
The final property is info
. Info is a dictionary object that allows for various non-image information to be shared.
New to Python? Check this out: Python for Beginners: An Introductory Guide to Getting Started
Saving an Image
Opening an image was as simple as calling the open()
method. Saving an image in Python is just as simple. You simply call save()
and pass in the name you want used to save your image. This method will save the image in the format identified by the extension on the filename you pass in. Listing 3
opens the image.jpg
file used in Listing 1
and saves it as a gif file by changing the filename that is passed to save()
.
Listing 3: Saving a File in Python
from PIL import Image
image = Image.open('image.jpg')
image.save("NewImage.gif")
The save()
method can receive a file object instead of a filename. It also can have two additional parameters passed that specifies the format you want used to save the file. The formats include BMP, DIB, EPS, GIF, ICO, JPEG, PCX, PNG, TIFF, and a multitude of others. The format would be specified as the second parameter as such:
image.save("NewImage", format="BMP")
Summary
Python is and will continue to be very popular. This was a relatively short article because it is relatively easy to work with images in Python! You learned how to open an image in Python. You also saw how to obtain information about the image and how to then save a copy of the image in a different format.
Opinions expressed by DZone contributors are their own.
Comments