PIL thumbnails unreasonably large

A

Almad

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
 
A

Almad

Forgot to add: I'm using PIL 1.1.5 with Python 2.4, expected on both
Gentoo Linux and Windows XP.
 
L

Larry Bates

Almad said:
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
 
F

Fredrik Lundh

Almad said:
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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top