Pillow Image Resize

In this chapter, you will learn how to use Pillow to resize an image. You will also learn how to create a thumbnail from an image. Lastly, you will learn the difference between resize() and thumbnail() methods.

Resizing Images

You can resize images by calling the Pillow’s resize() method on an object of the Image class. The size of the resized image (width, height) must be passed as a tuple to resize(). This will return the resized image as a new object of the Image class.

Syntax:

image_object.resize((width, height))

Example

# Resizing the old image to 100*200 pixels and storing the new image as img_2
img_2 = img.resize((100, 200))

# Printing the size of img_2 as width, height
print(img_2.size)
# Outputs- (100, 200)

# Display the new image
img_2.show()

The Output of this will be-

Resized Image using pillow resize method

Example

You can also resize the new image to a multiple or a factor of the height and width of the original image. In this example, we will reshape the new image to half the height and width of the original image.

# Resizing the old image to to half the height and width and storing the new image as img_half
img_half = img.resize((img.width//2, img.height//2))

# Printing the size of img and img_half

print(img.size)
# Outputs- (3264, 2448)
print(img_half.size)
# Outputs- (1632, 1224)

Creating Thumbnails from Images

You can create thumbnails from images by using the Pillow’s thumbnail() method on an object of the Image class. The size of the thumbnail (width, height) must be passed to thumbnail() as an argument. The thumbnail() converts the image into a thumbnail in place. Hence, the original image is changed to a thumbnail by the thumbnail() method.

thumbnail() preserves the original aspect ratio (ratio of width to height) of the image. The resulting thumbnail, therefore, has the largest possible size that-

  • Preserves the aspect ratio of the original image.
  • No dimension of the thumbnail is greater than the desired size.

Syntax:

image_object.thumbnail((width, height))

Example

In the following example, we create a thumbnail from the image img. The size of the thumbnail will be the largest size that preserves the aspect ratio such that no side of the image is greater than the desired size of 100 pixels.

# Convert the image to a thumbnail in-place
img.thumbnail((100, 100))

# Print size of img
print(img.size)
# Outputs- (100, 75)

img.show()

The output of this will be-

Created thumbnail using pillow thumbnail function

Difference between resize() and thumbnail()

Both resize() and thumbnail() change the size of the image. However, there are some differences between them. These differences are illustrated in the table below.

resize()thumbnail()
Can be used to upscale(increase the size of the image).Cannot be used to upscale.
Does not preserve the aspect ratio.Preserves the aspect ratio.
Does not make changes to the original image. Returns a new Image object after resizing.Makes changes to the original image.
The size of the new image is exactly the same as the required size.The size of the new image is the largest it can be by preserving the aspect ratio while no side of the thumbnail is greater than the desired size of the thumbnail.