Predicting Thumbnail Sizes from PIL
fredrik at pythonware.com
Sun Sep 11 15:34:07 CEST 2005
> I need to calculate the thumbnail sizes PILL will produce from an image.
> In most cases I can divide and round by adding .5, but PIL seems to
> round differently on odd sized images.
> For example, I want to reduce an 800x816 image to have a maximum size of
> 697. (697/816) * 800 = 683.33, so my rounding results in 683. But PIL
> produces an image of 684x697.
the algorithm is:
x, y = im.size
if x > size: y = max(y * size / x, 1); x = size
if y > size: x = max(x * size / y, 1); y = size
size = x, y
that is, it shrinks the image horizontally first, and it then shrinks the
resulting image vertically:
>>> 816 * 697 / 800
>>> 697 * 697 / 710
More information about the Python-list