469,918 Members | 2,126 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,918 developers. It's quick & easy.

PIL thumbnails unreasonably large

Hello,

I wonder how do I create reasonable thumbnails from JPEG with PIL.
My code:

logging.debug('Downloading image %s' % id)
uri = ''.join([config['photo']['masterpath'], '?p=',
str(id)])
uf = urlopen(uri).read()
f = tmpfile()
f.write(uf)
f.seek(0)
logging.debug('Resizing image %s' % id)
img = Image.open(f)
prev = img.copy()
img.thumbnail((180,180))
if prev.size[0] > 640 or prev.size[1] > 640:
prev.thumbnail((640,640))
# save resized to temporary files
f.seek(0)
img.save(f, "JPEG", quality=50)
fp = tmpfile()
prev.save(fp, "JPEG", quality=200)

Well, works fine, but img size is about 0,5 MB (!), but strangely, prev
one is around 200 kb.

How do I change this? I tried to play with various quality settings,
but with no effect. img thumbnail is supposed to be < 10 kb...

Thank You,

Almad

May 10 '06 #1
4 2808
Forgot to add: I'm using PIL 1.1.5 with Python 2.4, expected on both
Gentoo Linux and Windows XP.

May 10 '06 #2
Almad wrote:
Hello,

I wonder how do I create reasonable thumbnails from JPEG with PIL.
My code:

logging.debug('Downloading image %s' % id)
uri = ''.join([config['photo']['masterpath'], '?p=',
str(id)])
uf = urlopen(uri).read()
f = tmpfile()
f.write(uf)
f.seek(0)
logging.debug('Resizing image %s' % id)
img = Image.open(f)
prev = img.copy()
img.thumbnail((180,180))
if prev.size[0] > 640 or prev.size[1] > 640:
prev.thumbnail((640,640))
# save resized to temporary files
f.seek(0)
img.save(f, "JPEG", quality=50)
fp = tmpfile()
prev.save(fp, "JPEG", quality=200)

Well, works fine, but img size is about 0,5 MB (!), but strangely, prev
one is around 200 kb.

How do I change this? I tried to play with various quality settings,
but with no effect. img thumbnail is supposed to be < 10 kb...

Thank You,

Almad

JPEG quality 200 is overkill, btw -- it completely disables JPEG's
quantization stage, and "mainly of interest for experimental pur-
poses", according to the JPEG library documentation, which
continues:

"Quality values above about 95 are NOT recommended for
normal use; the compressed file size goes up dramatically
for hardly any gain in output image quality."

-Larry Bates
May 10 '06 #3
Almad wrote:
I wonder how do I create reasonable thumbnails from JPEG with PIL.
My code:

logging.debug('Downloading image %s' % id)
uri = ''.join([config['photo']['masterpath'], '?p=',
str(id)])
uf = urlopen(uri).read()
f = tmpfile()
f.write(uf)
f.seek(0)
logging.debug('Resizing image %s' % id)
img = Image.open(f)
prev = img.copy()
img.thumbnail((180,180))
if prev.size[0] > 640 or prev.size[1] > 640:
prev.thumbnail((640,640))
# save resized to temporary files
f.seek(0)
img.save(f, "JPEG", quality=50)
fp = tmpfile()
prev.save(fp, "JPEG", quality=200)

Well, works fine, but img size is about 0,5 MB (!), but strangely, prev
one is around 200 kb.


seek(0) doesn't truncate the file. if you want a small thumbnail file, you
shouldn't write it to the beginning of a large file.

try calling the truncate method after you've saved the file:

img.save(f, "JPEG", quality=50)
f.truncate()

(a better solution would be to wrap the data in a StringIO object, and
write the thumbnail into a fresh temp file -- or another StringIO object:

uf = urlopen(uri).read()
img = Image.open(StringIO.StringIO(uf))
...
f = tmpfile()
img.save(f, "JPEG")
...

)

</F>

May 10 '06 #4
Thank You! That helped.

Somehow obvious when you pointed out, but...*phew*.

Almad

May 11 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Lemming | last post: by
5 posts views Thread by Ken | last post: by
11 posts views Thread by callieandmark | last post: by
reply views Thread by =?Utf-8?B?TWF0dA==?= | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.