Django newbie :) I'm wondering if you *kind people* could help me work out 
how to solve a little issue I'm having :) I'm using *S3 storage* via the 
package django-storages <http://django-storages.readthedocs.org/en/latest/>.

The following code gets stuck in an endless loop. I know  just enough to 
know why, but not how to solve...

*Remembering I'm a newbie*....


This is what the the code I have written does so far....

1) profile gets saves and image uploaded *(without the resize of course)*to S3 
- 
*PERFECT*
2) the full size image is then read from S3 in the image_resize function 
and resized and renamed in the database - *PERFECT*
3) old large image get deleted from S3 - - *PERFECT*
4) it is then saved on this line   *self.image.save *and continues it all 
over again in a *infinite* loop! - ERROR  


        def save(self, *args, **kwargs):
            # delete old file when replacing by updating the file
            try:
                this = Profile.objects.get(id=self.id)
                if this.image != self.image:
                    this.image.delete(save=False)
            except: pass # when new photo then we do nothing, normal case
            super(Profile, self).save(*args, **kwargs)
            if self.image:
                image_resize(self)
    
    
    def image_resize(self):
        import urllib2 as urllib
        from cStringIO import StringIO
        from django.core.files.uploadedfile import SimpleUploadedFile
    
        '''Open original photo which we want to resize using PIL's Image 
object'''
        img_file = urllib.urlopen(self.image.url)
        im = StringIO(img_file.read())
        resized_image = Image.open(im)
    
    
        '''Convert to RGB if necessary'''
        if resized_image.mode not in ('L', 'RGB'):
            resized_image = resized_image.convert('RGB')
    
        '''We use our PIL Image object to create the resized image, which 
already
        has a thumbnail() convenicne method that constrains proportions.
        Additionally, we use Image.ANTIALIAS to make the image look better.
        Without antialiasing the image pattern artificats may reulst.'''
        resized_image.thumbnail((100, 100), Image.ANTIALIAS)
    
        '''Save the resized image'''
        temp_handle = StringIO()
        resized_image.save(temp_handle, 'jpeg')
        temp_handle.seek(0)
    
    
        ''' Save to the image field'''
        suf = 
SimpleUploadedFile(os.path.split(self.image.name)[-1].split('.')[0],
                                     temp_handle.read(), 
content_type='image/jpeg')
        self.image.save('%s.jpg' % suf.name, suf, save=True)



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


Reply via email to