matthewwithanm / pilkit

Utilities and processors built for, and on top of PIL
BSD 3-Clause "New" or "Revised" License
196 stars 54 forks source link

Fix _format_to_extension for 'PNG' format #38

Closed kravemir closed 5 years ago

kravemir commented 5 years ago

Do not return 'apng' extension for 'PNG' format.

@vstoykov I've prepared the fix for this problem https://github.com/matthewwithanm/pilkit/pull/32#issuecomment-461177013:

Yes the PR is looking good. One more thing. Can you rebase against master to see if the problem with the tests will still be here?

@vstoykov I did rebase it in different branch, but fails anyway: https://travis-ci.org/kravemir/pilkit/jobs/489724487

I debugged pilkit.utils._format_to_extension: https://github.com/matthewwithanm/pilkit/blob/f1e17e58b5a6e8b03d3097d94011f41c4f424e8e/pilkit/utils.py#L61-L75

And, after few method invocations (debug: Resume program (F9)), watch Image.EXTENSION has got value:

<class 'dict'>: {'.bmp': 'BMP', '.gif': 'GIF', '.tif': 'TIFF', '.tiff': 'TIFF', '.jfif': 'JPEG', '.jpe': 'JPEG', '.jpg': 'JPEG', '.jpeg': 'JPEG', '.pbm': 'PPM', '.pgm': 'PPM', '.ppm': 'PPM', '.png': 'PNG', '.apng': 'PNG', '.blp': 'BLP', '.bufr': 'BUFR', '.cur': 'CUR', '.pcx': 'PCX', '.dcx': 'DCX', '.dds': 'DDS', '.ps': 'EPS', '.eps': 'EPS', '.fit': 'FITS', '.fits': 'FITS', '.fli': 'FLI', '.flc': 'FLI', '.ftc': 'FTEX', '.ftu': 'FTEX', '.gbr': 'GBR', '.grib': 'GRIB', '.h5': 'HDF5', '.hdf': 'HDF5', '.jp2': 'JPEG2000', '.j2k': 'JPEG2000', '.jpc': 'JPEG2000', '.jpf': 'JPEG2000', '.jpx': 'JPEG2000', '.j2c': 'JPEG2000', '.icns': 'ICNS', '.ico': 'ICO', '.im': 'IM', '.iim': 'IPTC', '.mpg': 'MPEG', '.mpeg': 'MPEG', '.mpo': 'MPO', '.msp': 'MSP', '.palm': 'PALM', '.pcd': 'PCD', '.pdf': 'PDF', '.pxr': 'PIXAR', '.psd': 'PSD', '.bw': 'SGI', '.rgb': 'SGI', '.rgba': 'SGI', '.sgi': 'SGI', '.ras': 'SUN', '.tga': 'TGA', '.webp': 'WEBP', '.wmf': 'WMF', '.emf': 'WMF', '.xbm': 'XBM', '.xpm': 'XPM'}

As you can see, there are multiple entries for PNG format: ... '.png': 'PNG', '.apng': 'PNG' ....

Therefore, the result of _format_to_extension is unstable, because depends on order of items in the dict (ie. on order of serving them for for k, v in Image.EXTENSION.items() iteration).