473,796 Members | 2,460 Online
Bytes | Software Development & Data Engineering Community
+ 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).re ad()
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 2954
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).re ad()
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).re ad()
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).re ad()
img = Image.open(Stri ngIO.StringIO(u f))
...
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
5195
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
6850
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 I might want to try to counter the problems I seem to be facing... The only area of the project I am struggling on is the graphical side. Basically, I need to take in a load of large photographs, create a thumbnail for each, create a preview...
5
3672
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 address this situation. Where did I go wrong?
1
587
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 and extract thumbnails from each jpg file but nothing about what should happen to the extracted thumbnail. Ideally I would like each thumbnail to be displayed (with filename underneath each thumbnail) in a listview control (large icon view).
11
1675
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 thumbnail dosen't show. Any ideas ? Thanks for any advice.
0
1553
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 a function(getMyIcon()) which interacts with shell to get icons and updates the two imagelists( for small and large icons).this in turn is used to update a listview to show the file/folder name and the icons. The problem is the UI becomes...
2
2089
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 20kb of 200 X 200 px. Is this possible? If yes, pls let me know. Thanks in advance
0
835
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 display the full image. I was thinking about populating a gridView with the thumbnails...but i would sooner no t have to store all the thumnail images into the db aswell. Iam fairly new to asp.net so any help would be appreciated. Thanks in...
1
1958
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 for the product. The 6 thumbnails are the next 6 images for the product from the data table. I would like the user to click on the thumbnail and that thumbnail would show in the 1 large image area.
0
9683
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9529
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10457
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10231
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10176
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10013
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5443
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5576
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2927
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.