Pillow (successor to PIL) is often referred to as the standard for image processing in Python. A common task in Pillow is resizing an image. Fortunately Pillow provides various ways to do this which we discuss below.
thumbnail()
thumbnail()
is a method on a Pillow Image
object itself, and a common option for resizing images. To use this method we provide a tuple with the maximum width and height. The thumbnail()
method will maintain the aspect ratio of the original image, meaning that the final result could be smaller (on one axis) than the size provided. If the width and height provided to the method does not maintain the aspect ratio of the original image, the size of one axis is reduced until the original aspect ratio is acheived.
Be aware that the thumbnail()
method can only reduce the size of an image. Also, thumbnail()
will alter the image object in place (the image object in memory, not image file on disk), and the method will return None
.
from PIL import Image, ImageOps
# Open the image, view size
image = Image.open("dog.jpeg")
image.size
# alter image, view size
image.thumbnail((150, 250))
image.size
Before | After |
---|---|
![]() |
![]() |
Size: 200x200 | Size: 150x150 |
contain()
The contain()
function from the ImageOps
module is similar to thumbnail()
in that it requires a maximum width and height and will maintain the aspect ratio of the original image. This means that the final image result could be smaller (on one axis) than the size provided. If the width and height provided to the function does not maintain the aspect ratio of the original image, the size of one axis is reduced until the original aspect ratio is acheived.
contain()
will not change the original image object (in memory), but will return a newly resized image object. Also, contain()
can be used to increase or decrease the size of an image.
from PIL import Image, ImageOps
# Open the image, view size
image = Image.open("dog.jpeg")
image.size
# create new image, view size
new_image = ImageOps.contain(image, (150, 250))
new_image.size
Before | After |
---|---|
![]() |
![]() |
Size: 200x200 | Size: 150x150 |
cover()
The cover()
function from the ImageOps
module is similar to contain()
but instead requires tuple with the minium width and height sizes and will maintain the aspect ratio of the original image. This means that the final image result could be larger (on one axis) than the size provided. If the size tuple provided to the function does not maintain the original aspect ratio, the size of one image axis will be increased to keep this ratio.
cover()
will not change the original image object (in memory), but will return a newly resized image object. Also, cover()
can be used to increase or decrease the size of an image.
from PIL import Image, ImageOps
# Open the image, view size
image = Image.open("dog.jpeg")
image.size
# create new image, view size
new_image = ImageOps.cover(image, (150, 250))
new_image.size
Before | After |
---|---|
![]() |
![]() |
Size: 200x200 | Size: 250x250 |
fit()
The fit()
function from the ImageOps
module differs from the others in this tutorial in that it will not maintain the original image aspect ratio. It will resize the image to the size provided.
fit()
will not change the original image object (in memory), but will return a newly resized image object. Also, fit()
can be used to increase or decrease the size of an image.
from PIL import Image, ImageOps
# Open the image, view size
image = Image.open("dog.jpeg")
image.size
# create new image, view size
new_image = ImageOps.fit(image, (150, 250))
new_image.size
Before | After |
---|---|
![]() |
![]() |
Size: 200x200 | Size: 150x250 |
pad()
The pad()
function from the ImageOps
module works like a combination of contain()
and fit()
functions. The pad() function requires the desired width and height of the resized image. The image aspect ratio will be maintained in a similar way as contain()
. If the resized image is smaller than the desired size on one axis, padding is added on that axis until the the image matches the width and height passed to the function.
This allows Pillow to return the exact size provided to the function, while maintaining the aspect ratio of the original image. The default padding color is black, but other colors can be passed to the function as well.
from PIL import Image, ImageOps
# Open the image, view size
image = Image.open("dog.jpeg")
image.size
# create new image, view size
new_image = ImageOps.pad(image, (150, 250), color=(0, 153, 0))
new_image.size
Before | After |
---|---|
![]() |
![]() |
Size: 200x200 | Size: 150x250 |
pad()
will not change the original image object (in memory), but will return a newly resized image object. Also, pad()
can be used to increase or decrease the size of an image.