473,327 Members | 1,920 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

PIL & image size reduction script

Hi,

Thanks to the NG, I got the script hereunder working.

1) I am not certain that the call to convert does much (checking the doc)
2) Can this be improved as far as the final image size in (X,Y) ?
For instance, passing a large .jpg with a target byte size of 7000, I get
final (X,Y) results around (213, 174) ... but might want to strech it a bit
while keeping the byte size.

Thanks,

Philippe

#************************************************* ******************************
def Image_Reduce(self, p_filename, p_size):
BASE_SIZE = 400.0
l_im = Image.open(p_filename)

l_size = l_im.size

if l_size[0] > l_size[1]:
l_ratio = BASE_SIZE / l_size[0]
l_x_size = l_ratio * l_size[0]
l_y_size = l_ratio * l_size[1]
else:
l_ratio = BASE_SIZE / l_size[1]
l_x_size = l_ratio * l_size[0]
l_y_size = l_ratio * l_size[1]
# l_im.show()
l_image = l_im.resize( (l_x_size, l_y_size))
l_image = l_image.convert(mode="RGB", palette=Image.ADAPTIVE)

l_done = False

l_tmp_file_name = 'sc_tmp_file.jpg'

while False == l_done:
l_image.save(l_tmp_file_name)
l_st = os.stat(l_tmp_file_name)
print 'HERE ', l_st
if p_size < l_st[6]:

l_ratio -= 0.005
print 'NEW RATIO = ', l_ratio
l_x_size = l_ratio * l_size[0]
l_y_size = l_ratio * l_size[1]
l_image = l_im.resize( (l_x_size, l_y_size))

else:
l_done = True
l_image.show()
print l_image.size
Mar 26 '06 #1
5 5412
Philippe Martin wrote:
Hi,

Thanks to the NG, I got the script hereunder working.

1) I am not certain that the call to convert does much (checking the doc)
I think you only need it if your source image comes in a format that
can't be stored into a jpeg file (e.g. 8-bit paletted). You'll need
that if you're converting from GIF files, for example. It shouldn't
hurt otherwise.
2) Can this be improved as far as the final image size in (X,Y) ?
I'm not sure if I get you: You tell the image object it's new
(X,Y)-size in the resize method, don't you?
For instance, passing a large .jpg with a target byte size of 7000, I get
final (X,Y) results around (213, 174) ... but might want to strech it a bit
while keeping the byte size.


If I got you right, you want to compress the image to a certain file
size. Maybe you should try optimizing the additional save parameters
for the jpeg encoder.
(http://www.pythonware.com/library/pi...k/formats.htm). Try
reducing the "quality" parameter.
l_image.save(l_tmp_file_name, quality=25)

Hope this helps.

Niki

Mar 27 '06 #2
nikie wrote:
Philippe Martin wrote:
Hi,

Thanks to the NG, I got the script hereunder working.

1) I am not certain that the call to convert does much (checking the doc)
I think you only need it if your source image comes in a format that
can't be stored into a jpeg file (e.g. 8-bit paletted). You'll need
that if you're converting from GIF files, for example. It shouldn't
hurt otherwise.


OK
2) Can this be improved as far as the final image size in (X,Y) ?
I'm not sure if I get you: You tell the image object it's new
(X,Y)-size in the resize method, don't you?


Yes, I keep reducing the (X,Y) size as long as the file is too large (in
bytes)
For instance, passing a large .jpg with a target byte size of 7000, I get
final (X,Y) results around (213, 174) ... but might want to strech it a
bit while keeping the byte size.
If I got you right, you want to compress the image to a certain file
size. Maybe you should try optimizing the additional save parameters
for the jpeg encoder.
(http://www.pythonware.com/library/pi...k/formats.htm). Try
reducing the "quality" parameter.
l_image.save(l_tmp_file_name, quality=25)


That might be it !!!

Thanks,

Philippe

Hope this helps.

Niki


Mar 27 '06 #3
PS: where can I find those extra parameters in the doc (ex: quality) ... I
must be blind.

Philippe

nikie wrote:
Philippe Martin wrote:
Hi,

Thanks to the NG, I got the script hereunder working.

1) I am not certain that the call to convert does much (checking the doc)


I think you only need it if your source image comes in a format that
can't be stored into a jpeg file (e.g. 8-bit paletted). You'll need
that if you're converting from GIF files, for example. It shouldn't
hurt otherwise.
2) Can this be improved as far as the final image size in (X,Y) ?


I'm not sure if I get you: You tell the image object it's new
(X,Y)-size in the resize method, don't you?
For instance, passing a large .jpg with a target byte size of 7000, I get
final (X,Y) results around (213, 174) ... but might want to strech it a
bit while keeping the byte size.


If I got you right, you want to compress the image to a certain file
size. Maybe you should try optimizing the additional save parameters
for the jpeg encoder.
(http://www.pythonware.com/library/pi...k/formats.htm). Try
reducing the "quality" parameter.
l_image.save(l_tmp_file_name, quality=25)

Hope this helps.

Niki


Mar 27 '06 #4
Philippe Martin wrote:
PS: where can I find those extra parameters in the doc (ex: quality) ... I
must be blind.

In the http://www.pythonware.com/library/pi...ok/formats.htm page.
Look in the "JPEG" section. Different file formats support different
options.

Mar 27 '06 #5
Many thanks
nikie wrote:
Philippe Martin wrote:
PS: where can I find those extra parameters in the doc (ex: quality) ...
I must be blind.

In the http://www.pythonware.com/library/pi...ok/formats.htm page.
Look in the "JPEG" section. Different file formats support different
options.


Mar 27 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: ËιúÐû | last post by:
How to get the image size with VBScript in ASP?
38
by: Shaun McKinnon | last post by:
HI...Here's my problem...I have a popup window that loads when i want it to, but it's not sized properly. I've set the size, but it doesn't seem to work. I've been on 8 different websites to find...
4
by: Kenny | last post by:
I have been trying to write a script that will increase the size of and image when you mouse over it, and decrease it to original size, when you mouse out. After a couple of attempts, this is what...
7
by: Haines Brown | last post by:
I'm in the practice of specifying img width and height in my style definitions, although I usually use the actual size of the image. I'm reworking the style and it would be convenient to leave...
11
by: KarimL | last post by:
Thanks for your advices... but i need to get the Image height because i dynamically resize the height of my webcontrol based on the image height. More i just have the url (relative parth) to the...
3
by: Anurag | last post by:
Hi, I posted this on other user-groups. Hardly got any responses. Here goes the question: PLATFORM = DB2 UDB 8.2+ on AIX 5.x, DPF Two Topics broadly: (A) I wish to find out a rough...
9
by: klausklausenator | last post by:
Hi all, I have a php-generated webpage with images. My problem is: the Internet Explorer cannot calculate the correct image size. Because of this the images are not cached. So everytime a user...
2
by: abrtlt | last post by:
I am using Ajax with a PHP script to obtain the name of gif files from a MySQL database. Javascript then embeds the actual file in the web page, thus displaying the image on the fly. For precise...
5
by: johnric | last post by:
I can't seem to get firefox to read the image size in my javascript. It will work in IE and firefox but firefox will not read the image size. Can anyone see the problem in my code? I am new to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.