Image Editing with Python: the Pillow and Turtle Libraries
An overview of image editing in Python using the Pillow and Turtle libraries
 Python is well known as a highly intuitive and versatile programming language, but how good is it when it comes to editing images? Is it possible to easily and successfully manipulate graphics in this jack-of-all-trades language or have its critics found a weak spot?
Hardly. But let’s look at the evidence.
The Python language comes with its own native image editing library, as well as having access to a variety of external libraries that can be easily added. The Turtle image editing module comes as standard and is as easy and intuitive to learn as the rest of Python.
For more advanced functionality, the Pillow library is an easy add-on and is a cinch to master if you are already familiar with the Python language. This library was created specifically for Python and is continually being updated for additional use cases, efficiency and ease of use.
The Pillow and Turtle libraries: what’s the difference?
The Turtle library is a part of the Python language and comes integrated at download. Its functionality is mainly concerned with drawing shapes onto a canvas with a virtual pen. This doesn’t sound like much but with a little thought, some recursion, and a few parameters these designs can become very elaborate.
The Pillow library isn’t a part of Python but can be downloaded and installed for free. There is also an enterprise edition that offers greater support and other benefits but if you want to start learning and developing you can go ahead with this. Pillow allows you to do more high-level editing such as cropping, resizing, and other functions you would associate with a commercial image editor.
What can you do with Turtle?
Turtle has a long list of functions that are easily called on within the code. The onscreen pen used for drawing shapes and pictures is affectionately called the ‘turtle’ and thus the cute name. The original idea was to provide a simple library that would help new coders to become familiar with Python in a way that was fun and intuitive.
The design was so successful that this library is often used as a way to get young coders interested in the language and get their feet wet with programming. Every programmer remembers the thrill of seeing their first output on screen and the Turtle library delivers these ‘aha’ moments effortlessly.
Despite the excellent reputation it has as an educational tool for children, the turtle library still has its uses for adults and working developers as well as a handy go-to for simple paint and draw functionality.
To start using turtle in your version of Python requires a single line of code:
import turtle
It’s really that simple. Now all of the built-in functions are ready to use.
Common functions from the turtle library
- Turtle(): the top-level function to create and return a turtle object
- forward(value): move the turtle forward to the limit of the value provided
- backward (value): move the turtle backwards to the limit of the value
- penup(): pick up the turtle drawing pen
- pendown(): put the turtle pen down
- color(): change the drawing colour of the turtle pen
As you can see, these functions are extremely intuitive and can get either you, your child, or your student creating tangible, visible output on screen in a matter of minutes.
Now let’s say you want to create something a little more elaborate than a colored line, how would you go about, for example, creating a star shape?
Assuming the turtle library is already imported, it might look something like this:
#initialize an instance of turtle my_star = turtle.Turtle() #set the number of sides to 5 number_of_sides = 5 #set the length of each side side_length = 100 #set the angle degree for each point in the star angle_degree = 144 #set up a loop with 5 iterations do draw the 5 sides of your star for i in range(number_of_sides): #draw a line of 100 units in length mystar.forward(side_length) #turn by 144 degrees before drawing the next side of the star mystar.right(angle_degree) #close your method turtle.done()
What can you do with Pillow?
The Pillow library allows a developer to get into the nitty-gritty detail of a graphic and code out step by step much of the magic we take for granted in image editing applications. Pillow is the updated version of what used to be known as the Python Imaging Library (PIL) and is the original graphics library for Python, so it works seamlessly with your code.
In fact, many of the other libraries available for Python are built on top of this one, so even if you are planning on using something with more bells and whistles, it can make sense to begin with Pillow to gain a deep understanding of what’s going on beneath the surface of these other applications.
Here are just a few of the things that Pillow enables you to do with Python:
- Create thumbnails
- Convert images between formats
- Print images
- Archive images
- Handle batch processing for images
- Display images using a wide range of GUI toolkits
- Resize images
- Rotate images
- Draw statistical data from a graphic using the histogram method
- Blur images
- Crop images
- Edit colour
- Create animations
- Read images
- A lot more besides the above list
 To install Pillow go to a command line on your computer and input the following
pip install pillow
Let’s see how importing an image works.
#import your image from PIL import Image img_filename = beautiful_image.jpg #load the image onscreen with Image.open(img_filename) as beautiful_img: beautiful_img.load() #Now let’s do something useful with that image. Maybe we need to crop it to conform to the dimensions of a social media website. #crop the image to the exact number of pixels you need beautiful_img = beautiful_img.crop((600, 0, 1450, 1345)) #display the cropped version on screen beautiful_img.show()
As soon as you are ready to get going, you can download the free or enterprise editions of Pillow right here.
When do programmers need to manipulate images?
We take a lot of things for granted on the web and free or freemium image manipulation sites and applications abound. These include everything from removing backgrounds to cropping resizing, compressing, and so on.
As a developer, you can’t always rely on free tools or off-the-shelf software to solve problems in your code, so it’s up to you to come up with integrated working solutions for the project at hand. If you are creating software that takes images as inputs and resizes them for a specific format, think dimensions for specific purposes, platforms, banner ads, thumbnails, etc, it isn’t practical or desirable to send out a call to an external program to do the work for you.
It may be hard to fit your exact needs, the program may go offline, it may not be there when you need it, or any number of other unforeseen problems may arise.
It’s far better practice in this case to write your own code to solve the task, and if you are using Python then you’re in luck. You have at the very least the native turtle module and if you need more sophisticated image manipulation it’s as easy as a quick import to have all the functionality of Pillow at your fingertips.
Here are some of the main reasons you may need to use an image editing library in your code:
- Your program needs to extract statistical data from a graph
- Your program requires functionality to resize large numbers of images to conform to recommended dimensions on a platform or website
- You are designing an image editing app
- You are writing educational software that teaches students how to manipulate computer graphics
- Your code needs to output data into a visual format
Which programming language is the best for graphics?
Almost every mature programming language comes equipped with a native image editing module or has access to multiple external libraries. The Python language is very well equipped to handle all of the basic needs of image editing as well as many more complex and specialized tasks, particularly in the area of data science. The utility of this increasingly popular language doesn’t, however, stop there.