Python / Pillow: How to scale an image
Use Image.resize, but calculate both width and height.
if image.width > 1028 or image.height > 1028:
if image.height > image.width:
factor = 1028 / image.height
else:
factor = 1028 / image.width
tn_image = image.resize((int(image.width * factor), int(image.height * factor)))
Noo need to reinvent the wheel, there is the Image.thumbnail
method available for this:
maxsize = (1028, 1028)
image.thumbnail(maxsize, PIL.Image.ANTIALIAS)
Ensures the resulting size is not bigger than the given bounds while maintains the aspect ratio.
Specifying PIL.Image.ANTIALIAS
applies a high-quality downsampling filter for better resize result, you probably want that too.