By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,427 Members | 1,354 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,427 IT Pros & Developers. It's quick & easy.

Predicting Thumbnail Sizes from PIL

P: n/a
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.

Is there an easy rule that will always work -- like adding .67 instead
of .5?

Roger
Sep 11 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Roger wrote:
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[0]: y = max(y * size[0] / x, 1); x = size[0]
if y > size[1]: x = max(x * size[1] / y, 1); y = size[1]
size = x, y

that is, it shrinks the image horizontally first, and it then shrinks the
resulting image vertically:
816 * 697 / 800 710 697 * 697 / 710

684

</F>

Sep 11 '05 #2

P: n/a
Fredrik Lundh wrote:
[snip..]

the algorithm is:

x, y = im.size
if x > size[0]: y = max(y * size[0] / x, 1); x = size[0]
if y > size[1]: x = max(x * size[1] / y, 1); y = size[1]
size = x, y

that is, it shrinks the image horizontally first, and it then shrinks the
resulting image vertically:
>>> 816 * 697 / 800 710 >>> 697 * 697 / 710
684


Thanks ! This was also helpful to me.

Fuzzyman
http://www.voidspace.org.uk/python
</F>


Sep 11 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.