aamalig / django-profile

Automatically exported from code.google.com/p/django-profile
BSD 2-Clause "Simplified" License
0 stars 0 forks source link

Don't hardcode JPG format for thumbnail save (w/ patch) #78

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Simple fix to let the thumbnailing code save in the format of the image, 
instead of hardcoding 'JPEG', which 
will fail for any non-JPG thumbnail.

Patch in SVN diff format...

Index: userprofile/templatetags/avatars.py
===================================================================
--- userprofile/templatetags/avatars.py (revision 434)
+++ userprofile/templatetags/avatars.py (working copy)
@@ -84,7 +84,7 @@
             thumb = Image.open(ContentFile(avatar.read()))
             thumb.thumbnail((self.size, self.size), Image.ANTIALIAS)
             f = StringIO()
-            thumb.save(f, "JPEG")
+            thumb.save(f, thumb.format)
             f.seek(0)
             storage.save(filename, ContentFile(f.read()))

Original issue reported on code.google.com by daemianmack@gmail.com on 27 Mar 2010 at 7:39

GoogleCodeExporter commented 8 years ago
When image is saved after the user choose it, it's always in jpg format, so 
when it's
resized, there should be no problem

By the way i made a little patch in [443] to correctly handle non jpg images 
(but
finally always saved as jpg), by changing the mode to RGB (in first uploading or
later in resizing)

I tried with jpg, png and gif, and it was ok.

Could you try with this new trunk ?

Original comment by stephane.angel on 26 Apr 2010 at 4:18

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
i wanted to say in r443

Original comment by stephane.angel on 26 Apr 2010 at 4:27

GoogleCodeExporter commented 8 years ago
Thanks for looking at this. 

Saving everything as JPG really isn't ideal, as JPG isn't suitable for many 
image types: e.g., GIF is a better format for smaller images, images with a lot 
of text 
or sharp lines, and screenshots. 

A lot of GIFs end up looking worse after being saved to JPG, and end up with a 
larger filesize.

Lastly, any GIFs a user uploads that have transparency will look surprising to 
the user after the save to JPG.

There are additional reasons not to convert all image formats to JPG, but these 
are the ones most compelling to my needs. 

If you agree, the above patch as submitted is pretty incomplete, as there are a 
number of other codepaths that need to be updated to not hardcode JPG; I'll be 
happy to create a diff against my edits and apply here if you're amenable.

Original comment by daemianmack@gmail.com on 26 Apr 2010 at 4:53

GoogleCodeExporter commented 8 years ago
I agree with you, but i have not the time to make patches and tests for this, 
but if
you have a patch... (did you forgot to join it ?)

Original comment by stephane.angel on 26 Apr 2010 at 6:33

GoogleCodeExporter commented 8 years ago
Nope, just making sure you were interested. :)

Here's the full patch, inclusive of the single change I submitted above...

Index: views.py                                                                 

===================================================================
--- views.py    (revision 434)                                                  

+++ views.py    (working copy)                                                  

@@ -247,10 +254,11 @@
                 thumb.thumbnail((480, 480), Image.ANTIALIAS)                                                                                                                                                                                 
                 thumb.convert("RGB")                                                                                                                                                                                                         
                 f = StringIO()                                                                                                                                                                                                               
-                thumb.save(f, "JPEG")                                          

+                thumb.save(f, thumb.format)                                    

                 f.seek(0)                                                                                                                                                                                                                    
                 avatar = Avatar(user=request.user, image="", valid=False)                                                                                                                                                                    
-                avatar.image.save("%s.jpg" % request.user.username, 
ContentFile(f.read()))                                                          

+                file_ext = image.content_type.split("/")[1] # "image/gif" => 
"gif"                                                                           

+                avatar.image.save("%s.%s" % (request.user.username, file_ext), 
ContentFile(f.read()))                                                          

                 avatar.save()                                                                                                                                                                                                                

                 signal_responses = signals.post_signal.send(sender=avatarchoose, request=request, form=form)                                                                                                                                 
@@ -310,16 +319,23 @@

             if hasattr(settings, "AWS_SECRET_ACCESS_KEY"):                                                                                                                                                                                   
                 f = StringIO()                                                                                                                                                                                                               
-                image.save(f, "JPEG")                                          

+                image.save(f, image.format)                                    

                 f.seek(0)                                                                                                                                                                                                                    
                 avatar.image.delete()                                                                                                                                                                                                        
-                avatar.image.save("%s.jpg" % request.user.username, 
ContentFile(f.read()))                                                          

+                file_ext = image.content_type.split("/")[1] # "image/gif" => 
"gif"                                                                           

+                avatar.image.save("%s.%s" % (request.user.username, file_ext), 
ContentFile(f.read()))                                                          

             else:                                                                                                                                                                                                                            
                 image.save(avatar.image.path)                                                                                                                                                                                                

             avatar.valid = True                                                                                                                                                                                                              
Index: templatetags/avatars.py                                                  

===================================================================
--- templatetags/avatars.py     (revision 434)                                  

+++ templatetags/avatars.py     (working copy)                                  

@@ -84,7 +87,7 @@
             thumb = Image.open(ContentFile(avatar.read()))                                                                                                                                                                                   
             thumb.thumbnail((self.size, self.size), Image.ANTIALIAS)                                                                                                                                                                         
             f = StringIO()                                                                                                                                                                                                                   
-            thumb.save(f, "JPEG")                                              

+            thumb.save(f, thumb.format)                                        

             f.seek(0)                                                                                                                                                                                                                        
             storage.save(filename, ContentFile(f.read()))                                                                                                                                                                                    

Original comment by daemianmack@gmail.com on 26 Apr 2010 at 11:11

GoogleCodeExporter commented 8 years ago
ok thanks i'll check that

Original comment by stephane.angel on 27 Apr 2010 at 9:19

GoogleCodeExporter commented 8 years ago
it's now ok in r445, thanks a lot for your patch

Original comment by stephane.angel on 29 Apr 2010 at 9:27

GoogleCodeExporter commented 8 years ago
Sure thing. Thanks for the app; it's been helpful! 

Time permitting, I've got some other quick fixes I plan to cherry-pick out of 
my repository and submit as patches.

Original comment by daemianmack@gmail.com on 29 Apr 2010 at 9:49

GoogleCodeExporter commented 8 years ago
i'm waiting for them :)

Original comment by stephane.angel on 29 Apr 2010 at 9:51