473,735 Members | 8,833 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(se lf, p_filename, p_size):
BASE_SIZE = 400.0
l_im = Image.open(p_fi lename)

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.A DAPTIVE)

l_done = False

l_tmp_file_name = 'sc_tmp_file.jp g'

while False == l_done:
l_image.save(l_ tmp_file_name)
l_st = os.stat(l_tmp_f ile_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 5435
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
9017
by: ËιúÐû | last post by:
How to get the image size with VBScript in ASP?
38
5083
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 out what i'm doing wrong, and so far it seems i'm doing it the right way. Here's my code...any suggestions would be appreciated. <script language="javascript"> <!-- window.open("256fx/index.htm", "", "height=400, width=600"); //-->
4
16213
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 I've come up with. In this example, there are 5 images (0 thru 4) all with a height of 80px. When you mouse over an image, it changes the growingImage variable equal to the number of the image you mouse over, when you mouse out, it sets it to 5,...
7
4474
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 image size unspecified unless I need to change its dispayed size. What, if any, are the reasons why an image size should be specified in a stylesheet if it will be displayed in its actual size? -- Haines Brown
11
2024
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 the image but in design time mode all variables concerning Server or Context ar not set ! ...so I can't use MapPath function to obtain the physical parth of the picture ... So my second question is how to retrieve the physical root path of the...
3
2352
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 correlation between the size of Used Pages in the database and its backup image size (Have to do some space calculation).
9
1499
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 clicks on some link all the images gets retransfered and the page loads very slow. You can check it if you right-click on an image and select properties. Is there a possibility to send the image and its actual size-information to the browser?
2
4752
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 positioning, I would like to know the gif image size. Is there a way to obtain it using Javascript or otherwise? (I know the size is indeed contained in the gif format, as I can see it with a binary editor...) Thanks! Andrew
5
3761
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 programming and not very good at it yet.: <HTML> <HEAD> <script language="JavaScript"> //-- Generate a random number between 0 and max. function randomNum(max) { var rNum=NaN while (isNaN(rNum)) { rNum=Math.floor(Math.random()*(max))
0
8785
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
9327
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
9251
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
9200
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
8201
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6747
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6049
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3273
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 we have to send another system
2
2739
muto222
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.