Tuesday, February 28, 2012

Re: Resize Image On Save is resizing all images in directory?

After a bit more debugging I have found that when the save method gets
called it loops through every record in the UserProfilePic table so
that is why
the self.profilepic.path gets assigned eg image1.jpg, image2.jpg and
when doing a resize it loops over them all opening each path from the
table reszing each image.

Can anyone explain why this works like that?

On Feb 28, 6:39 am, richard <pullenjenn...@gmail.com> wrote:
> Hi I have an upload form that uploads an image and on the save i
> resize the image. But when i do this images that are in the same
> directory seem to get resized aswell even though they have unique hash
> names? any help please.
>
> MODELS.py
>
> def upload_to(instance, old_filename):
>     import time
>     import hashlib
>     import os
>
>     extension = os.path.splitext(old_filename)[1]
>     filename = hashlib.md5(str(time.time())).hexdigest() + extension
>     return "photos/%s/%s" % (instance.userprofile.user_id, filename)
>
> class UserProfilePic(models.Model):
>     current = models.BooleanField()
>     userprofile = models.ForeignKey(UserProfile)
>     profilepic = models.ImageField(upload_to=upload_to)
>
>     def save(self):
>         from PIL import Image, ImageOps
>
>         super(UserProfilePic, self).save()
>
>         #create profile image
>         filename2 = self.profilepic.path
>         im2 = Image.open(filename2)
>         im2 = ImageOps.fit(im2, (170, 190), Image.ANTIALIAS,
> (0.5,0.5)).save(filename, "JPEG", quality=100)

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment