How do websites make those icons scale up and down with your screen size? By having multiple of the same image in different sizes. If you want to add your logo to your site in multiple different sizes, then you’ll need to learn how to do image resizing. Image resizing is always an annoying task. In this post, we’re going to learn not only how to resize images, but also how to crop them with Python.
In this post we’re going to cover:
- What is PIL?
- How to Crop an Image in Python with PIL
- How to Resize an Image in Python with PIL
- Using PIL to Save an Image
- Testing Our Image Crop, Resize, and Save Functions
- Summary of How to Resize, Crop, and Save Images in Python
What is PIL?
PIL stands for “Python Imaging Library”. It is an add-on library to Python for image processing. It was initially released in 1995 and discontinued in 2011. The current version of PIL that we use in this post was forked as “Pillow”. Pillow adds support for Python 3.
PIL, and subsequently Pillow, has a range of image manipulation tools. You can do per-pixel changes, masking and transparency, filtering, enhancement, adding text, and more. In this post, we will simply be using it to crop, resize, and save an image. The image we’re using is the word cloud background from this post on Word Clouds from Tweets.
Before we jump into the code, we have to install the library. We can do that with
pip install pillow. If you are using Anaconda, you can use
conda install pillow.
How to Crop an Image in Python with PIL
All of the code in this post belongs in one file. If you want to split it up, remember to import the
PIL library each time. For our uses, we only need the
Image object from
PIL. The first thing that we’re going to do is open up an image and assign it to a variable. Next, we’ll print out the size just for our info.
crop_image function takes five parameters. The first parameter is the image itself, we require this to be an Image object. Next are the coordinates for the upper left and lower right coordinates of the rectangle we want to crop. It’s weird that Image takes this as a 4-tuple instead of two 2-tuples, but that’s the way the cookie crumbles.
The order of the integers that we need to pass are the leftmost value, the uppermost value, the rightmost value, and the bottommost value that we want to crop. In our function, we simply call the
crop function of the Image object and pass a 4-tuple made from the integers passed in. We can show the image for clarity. At the end, we return the image so we can use it later.
from PIL import Image im = Image.open("./cloud_shape.png") width, height = im.size print(width, height) # left, upper combo gives the upper left corner coordinates # right, lower combo gives the lower right corner coordinates def crop_image(im: Image, left, upper, right, lower): im2 = im.crop((left, upper, right, lower)) im2.show() return im2
How to Resize an Image with PIL in Python
Next, let’s take a look at resizing images with Python. This function only takes 3 parameters. The image itself is the first parameter. The other two parameters are the resulting width and height that we want to resize the image to.
Similar to cropping, all we do here is call the
resize method. This method takes a tuple of the desired width and height of our image. Then we show the image for our info and return it for later use.
def resize_image(im:Image, width, height): im1 = im.resize((width, height)) im1.show() return im1
Using PIL to Save an Image in Python
Finally, let’s make a third function to save images in Python. This function takes two parameters, one is the Image itself, and the other is the name of the file we want to save the image to. Similarly to cropping and resizing, we use the Image object to save the image. All we do is pass the filename to the
save option to save the image to that file.
def save_image(im:Image, filename): im.save(filename)
Testing Our Image Crop, Resize, and Save Functions
Now that we have our three functions, let’s test them out. Let’s crop the image to a 210 by 210 square. The upper left corner we choose is the coordinate
(210, 210) and the bottom right corner coordinate is
(420, 420). For testing the resize command, we’ll resize to half the height and half the width. Note that you can also resize bigger (I also tested double the width and height).
Finally, we can test the image save function by passing in the resulting images and a filename. For this example, I just called the cropped image
cropped.png and the resized image
resized.png. You are not limited to PNG images though.
cropped = crop_image(im, 210, 210, 420, 420) resized = resize_image(im, width//2, height//2) save_image(cropped, "cropped.png") save_image(resized, "resized.png")
The images we got from the cloud image are shown below.
(210, 210) to
Summary of How to Crop, Resize, and Save Images in Python
In this post we took an introductory look at the
Pillow, library in Python. Even though there’s a name change, we still import PIL. The original PIL is no longer maintained and only supported through Python 2, which is obsolete.
After our brief introduction to Pillow, we looked at how to crop, resize, and save an image in that order. We learned that cropping an image requires four integer values that dictate the upper left and lower right corners. Meanwhile, resizing an image requires two integer values representing the new size in pixels. Finally, saving an image only requires one string parameter – the filename we’re saving to.
I run this site to help you and others like you find cool projects and practice software skills. If this is helpful for you and you enjoy your ad free site, please help fund this site by donating below! If you can’t donate right now, please think of us next time.