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 4 2930
Forgot to add: I'm using PIL 1.1.5 with Python 2.4, expected on both
Gentoo Linux and Windows XP.
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
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>
Thank You! That helped.
Somehow obvious when you pointed out, but...*phew*.
Almad This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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.
|
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...
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |