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.