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.
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...
- 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')
- Also, ...