Skip to content
Learnearn.uk » Python Unit Home » Editing Images with Python Pillow

Editing Images with Python Pillow

Introduction

Introduction

Python Pillow is a great image editing module that allows you to perform a number of image editing tasks using Python.

Examples of things you can do are:

  • Edit the image size and dimensions
  • Rotate an image
  • Change the brightness of an image
  • Edit the colors of individual pixels in an image

This is especially useful if you need to process a large number of images using the same process!

Installing Pillow

Pillow usually does not come installed as standard, so you may need to install it on your computer first to use it.

Here is the link for how to install it

Getting Started

Getting Started

Before we try to edit the image, we should first get a feel for the program.

Download the image below and save it to a folder on your computer. Open the CMD/Terminal program and navigate to the correct folder that you just saved the file to.

Opening Images

Once you are in the correct folder in the terminal open up the Python interactive interpreter by typing python or python3.

Inside the interpreter import the Image class from Pillow and load the image by typing:

from PIL import Image

im = image.open('dogememe.png')

If you have any problems opening the file check that you opened Python in the same directory as where the image is located (leave Python and type ls or dir to check you can see the image).

Investigate the image.

Try out the following code and see what each one does.

  • im.height
  • im.width
  • im.size
  • im.getbands()
  • im.getbbox()
  • im.getcolors()
  • im.getexif()
  • im.getextrema()
  • im.getpalette()
  • im.getpixel()
  • im.getdata()
  • im.gethistogram()

Image Data

Image Data

Try the following code

im.getdata()

What do you see? What do the numbers represent?