-
Notifications
You must be signed in to change notification settings - Fork 192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimizing images when uploading to the server #710
Comments
We will be probably using pillow for dealing with images. |
|
then on MacOSX it might be necessary to activate
PIL SETUP SUMMARYversion Pillow 3.0.0
|
testing code before implementing a solution. >>> from PIL import Image
>>> # setting a generic size
>>> size = (256,256)
>>> im3 = Image.open('/Users/karl/foobar.jpg')
>>> # display the image to check if it's the right one.
>>> im3.show()
>>> # display the exif
>>> im3._getexif()
{36864: ('0221',), 37121: ('\x01\x02\x03\x00',), 37378: (2.5260688216892597,), 36867: (u'2015:10:05 16:01:01',), 36868: (u'2015:10:05 16:01:01',), 41989: (32,), 40960: ('0100',), 37383: (5,), 37385: (32,), 37386: (3.85,), 41986: (0,), 271: u'Brand', 272: u'model_blah', 274: 6, 531: 1, 41495: (2,), 282: 72.0, 283: 72.0, 33434: (0.03333333333333333,), 34850: (2,), 40961: (1,), 34853: (576,), 34855: (400,), 296: 2, 41987: (0,), 33437: (2.4,), 305: u'6.1.6', 306: u'2015:10:05 16:01:01', 37377: (4.914738124238733,), 40962: (960,), 41990: (0,), 40963: (720,), 34665: 206, 37379: (2.2344952467179717,)}
>>> im3.thumbnail(size)
>>> im3.save('pic-thumb-test.jpg', 'JPEG')
>>> im4 = Image.open('pic-thumb-test.jpg')
>>> im4.show()
>>> # return an empty result. The tumbnail image doesn't have exif anymore.
>>> im4._getexif()
>>> I need to find out to optimize and remove the exif from the big one. |
OK. I guess I have all the parts to make this works: Saving a file with the same name is removing the EXIF automatically. >>> from PIL import Image
>>> im = Image.open('/Users/karl/Desktop/foobar.jpg')
>>> im._getexif()
{36864: ('0221',), 37121: ('\x01\x02\x03\x00',), 37378: (2.5260688216892597,), 36867: (u'2015:10:05 16:01:01',), 36868: (u'2015:10:05 16:01:01',), 41989: (32,), 40960: ('0100',), 37383: (5,), 37385: (32,), 37386: (3.85,), 41986: (0,), 271: u'Brand', 272: u'model_blah', 274: 6, 531: 1, 41495: (2,), 282: 72.0, 283: 72.0, 33434: (0.03333333333333333,), 34850: (2,), 40961: (1,), 34853: (576,), 34855: (400,), 296: 2, 41987: (0,), 33437: (2.4,), 305: u'6.1.6', 306: u'2015:10:05 16:01:01', 37377: (4.914738124238733,), 40962: (960,), 41990: (0,), 40963: (720,), 34665: 206, 37379: (2.2344952467179717,)}
>>> im.save('/Users/karl/Desktop/foobar.jpg', 'JPEG')
>>> im2 = Image.open('/Users/karl/Desktop/foobar.jpg')
>>> im2._getexif() It also reduces the size in weight for the same dimensions. ls -ahl ~/Desktop/foobar.jpg
# BEFORE
# -rw------- 1 karl staff 222K 24 aoû 07:43 /Users/karl/Desktop/foobar.jpg
# AFTER
# -rw------- 1 karl staff 70K 22 oct 15:32 /Users/karl/Desktop/foobar.jpg \o/ ok now that I have everything in place, we can start coding a bit. |
@miketaylr Do we want to keep the original format or do we want to save everything to jpg? I'm in favor of saving always as jpeg so we can save weight, but I might forget about some issues. |
IMO I think it's better to keep original format and resolution, so the original file. If in the futur we want to use png or jpeg or better resolution we have original file so better to improve what we want. |
@miketaylr I need to test more what the library does by default. The stripping of EXIF would not remove that much. For JPEG, I'm pretty sure there is compression going on. I haven't tested for png yet. I'll do that tomorrow probably. |
ok cool. I guess for JPEG, it applies a default compression. Another neat feature of Pillow and that we wanted is to have the real format compare to someone uploading a >>> img = Image.open(image_file)
>>> img.size
(486, 53)
>>> img.format
'PNG' Now I just have to figure out how to save the modified image into the |
to note also, that for JPEG in between the original version and the optimized version, from my tests, I didn't detect any weird compression artifacts. |
From http://pillow.readthedocs.org/en/latest/handbook/image-file-formats.html about JPEG on
And for PNG
|
For JPEG, 75 is a pretty good compromise between quality and size, IMO (especially for screenshots of website bugs, rather than user avatar uploads or a photography social media website). |
This will probably require installation of other things at the system level. You will need to have - libjpeg - webp - libpng
http://flask.pocoo.org/docs/0.10/patterns/fileuploads/#improving-uploads explains how we can set |
Oh cool! Thanks. |
@karlcow OK, #882 was just merged. I think that should make this task very simple for you now -- you can do your optimization magic 👀 Just read the docs, and it looks like we just need to pass the right params to |
\o/ ^_^ |
@karlcow, are you actively working on this? I might steal it from you if not -- but don't want to step on any work-in-progress code. |
ah nop nop wait. ^_^ I want to give it a stab. It's just that I was doing other things |
@karlcow cool, no issue. |
ok started to write pseudo code yesterday for images optim.
Other tests? |
(It sounds like some of these tests and scenarios are for #722, but maybe you're fixing both issues in a single pass)
|
huh. :) yup ok I will just push the commit for this one and work after on #722 THanks @miketaylr |
Ah!
Good things:
Bad thing
QUESTIONS / TODO
|
@karlcow is there any way to detect an animated gif and just not do anything for the time being? If so I'd suggest we ship for non-animated images first and come up with something more creative after that. |
Agreed with you here @karlcow. |
@miketaylr Yes already testing for this with |
We may want to optimize an image when it's being uploaded:
Discussed at Paris WebCompat meeting on September 2015 with @miketaylr
See https://github.com/webcompat/webcompat.com/blob/master/webcompat/api/uploads.py#L53
The text was updated successfully, but these errors were encountered: