473,473 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 2930
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Lemming | last post by:
Hi, I'm putting together a website for a local estate agent. One of the things they want to do is upload property descriptions/prices/etc. plus a picture of the property which is for sale.
0
by: Leonard Challis | last post by:
Good morning everyone I am currently doing quite a large project in VB6 and ACCESS for college, for a Photographers company assignment. I am posting here to see if anyone has any advice on what...
5
by: Ken | last post by:
I am in the process of designing my first web-site, and am having a problem with my picture gallery. My thumbnails are all different sizes, I would like them to be one size. The manual does not...
1
by: Paul | last post by:
Hi, I'm struggling to create an app that contains a window showing thumbnails of all. jpg files within a specified directory. I've been a couple of examples of how to iterate through a directory...
11
by: callieandmark | last post by:
I have a very simple file upload script which creates a thumbnail of the file (jpg) upon uploading. This works fine with small images, however, if i try to upload a file over about 1mb the...
0
by: twoSeven | last post by:
hi, Here is what i want to do. I am making a file and folder browser similar to Windows Explorer with a treeview and listview.. In that, i get icons and thumbnails of a particular file/folder from...
2
by: GK | last post by:
Hi all, I wish to know , how i can create thumbnails automatically using javascripts. It should reduce the size as well as pixel resolution (ie, a 1 mb image of 1280 x 800 px can be reduced to...
0
by: =?Utf-8?B?TWF0dA==?= | last post by:
Hello all i need some help, What i want to do is to create a browser based app that can display various thumbnails of large images stored in an sql db. The user can then click the thumbnail to...
1
by: MU | last post by:
Hello Can someone give me some ideas on the best way to do the following? I have a product page and it has 1 large image and 6 thumbnails. The main image is the first photo from a data table...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.