[Image-SIG] Bug to save original resized image
Ernesto Guevara
eguevara2012 at gmail.com
Thu Sep 15 01:14:49 CEST 2011
Hello!
I use Django 1.3 and have a signal pre_save() function to save
original_image and create your thumbnail. Here is the code:
class Photo(models.Model):
title = models.CharField(max_length=255)
pub_date = models.DateField(auto_now = True)
original_image = models.ImageField(upload_to='photos')
thumbnail_image = models.ImageField(upload_to="photos/thumbs/")
slug_photo = AutoSlugField(populate_from=('title'), unique=True,
max_length=255, overwrite=True)
def __unicode__(self):
return self.title
def get_absolute_url(self):
return "/imobiliaria/imovel/photo/%s/" % (self.slug_photo)
def photo_pre_save(signal, instance, sender, **kwargs):
i = instance.original_image
THUMBNAIL_SIZE = (128, 128)
image = Image.open(i)
if image.mode not in ('L', 'RGB'):
image = image.convert('RGB')
print image.size # size (1007, 1531)
basewidth = 200
wpercent = (basewidth/float(image.size[0]))
hsize = int((float(image.size[1])*float(wpercent)))
original = image.resize((basewidth,hsize), Image.ANTIALIAS)
original.save(os.path.split(i.path)[-1], format='jpeg') # why is save
with size (1007, 1531)?
print original.size # size (200, 304)
image.thumbnail(THUMBNAIL_SIZE, Image.ANTIALIAS)
# Save the thumbnail
temp_handle = StringIO()
image.save(temp_handle, 'jpeg')
temp_handle.seek(0)
# Save to the thumbnail field
suf = SimpleUploadedFile(os.path.split(i.name)[-1],temp_handle.read(),
content_type='image/jpeg')
instance.thumbnail_image.save(suf.name, suf, save=False)
signals.pre_save.connect(photo_pre_save, sender=Photo)
Here is the problem:
print image.size # size (1007, 1531)
basewidth = 200
wpercent = (basewidth/float(image.size[0]))
hsize = int((float(image.size[1])*float(wpercent)))
original = image.resize((basewidth,hsize), Image.ANTIALIAS)
original.save(os.path.split(i.path)[-1], format='jpeg') # why is save
with size (1007, 1531)?
print original.size # size (200, 304)
If i upload a 1.3MB image, i want resize to a small size in line:
original = image.resize((basewidth,hsize), Image.ANTIALIAS)
And then, save this original image size:
original.save(os.path.split(i.path)[-1], format='jpeg')
But this image still save in uploaded size (1007, 1531) instead (200, 304).
is a bug?
Thanks!
Regards!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/image-sig/attachments/20110914/70c1dc77/attachment-0001.html>
More information about the Image-SIG
mailing list