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, ...

Get Automate it! - Recipes to upskill your business now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.