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
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