PIL & image size reduction script

P

Philippe Martin

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
 
N

nikie

Philippe said:
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/pil/handbook/formats.htm). Try
reducing the "quality" parameter.
l_image.save(l_tmp_file_name, quality=25)

Hope this helps.

Niki
 
P

Philippe Martin

nikie said:
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
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)
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/pil/handbook/formats.htm). Try
reducing the "quality" parameter.
l_image.save(l_tmp_file_name, quality=25)

That might be it !!!

Thanks,

Philippe
 
P

Philippe Martin

PS: where can I find those extra parameters in the doc (ex: quality) ... I
must be blind.

Philippe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

PIL problems 1
PIL GIF transparency 1
merge two png pic 8
Pil Raw Image Viewer-- How to get image mode 0
PIL cutting off letters 4
PIL interpolation access 2
ChatBot 4
using PIL for anti-automation in ASP 4

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top