O'Reilly logo

Automate it! - Recipes to upskill your business by Chetan Giridhar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Resizing, cropping, and generating thumbnails

Operations such as resizing images and cropping them to get a selected part of the image are very common, but these operations may become tedious when tried programmatically. See how we can achieve these tasks.

Getting ready

In this recipe, we will use the Pillow library to resize and crop images. As we already have the Pillow module installed, we don't have to worry about any installations. Let's jump into doing stuff.

How to do it...

  1. First, let's look at how to resize an image to the given dimensions. Create a Python file, resize.py, and paste the following code snippet:
            from PIL import Image 
            img = Image.open('sunset.jpg') 
            resized = img.resize((256,256)) 
            resized.save('sunset-resize.jpg', 'jpeg') 
    
  2. Also, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required