detect 'edge' jpeg files without modifying imghdr.tests

This commit is contained in:
nath@home 2015-07-21 19:03:27 +02:00
parent b444ec496c
commit 106ea70fdb
No known key found for this signature in database
GPG key ID: 6777F12E17534B8E

View file

@ -270,24 +270,23 @@ def _sc_encode(gain, peak):
# Cover art and other images.
def _wider_test_jpeg(h, f):
def _wider_test_jpeg(data):
"""Test for a jpeg file following the UNIX file implementation which
uses the magic bytes rather than just looking for the bytes b'JFIF'
or b'EXIF' at a fixed position.
"""
if h[:2] == b'\xff\xd8':
if data[:2] == b'\xff\xd8':
return 'jpeg'
def _image_mime_type(data):
"""Return the MIME type of the image data (a bytestring).
"""
# imghdr provides access to the list of tests performed on an image to
# determine its mimetype
if not (_wider_test_jpeg in imghdr.tests):
imghdr.tests.append(_wider_test_jpeg)
kind = imghdr.what(None, h=data)
# This checks for a jpeg file with only the magic bytes (unrecognized by
# imghdr.what). imghdr.what returns none for that type of file, so
# _wider_test_jpeg is run in that case. It still returns None if it didn't
# match such a jpeg file.
kind = imghdr.what(None, h=data) or _wider_test_jpeg(data)
if kind in ['gif', 'jpeg', 'png', 'tiff', 'bmp']:
return 'image/{0}'.format(kind)
elif kind == 'pgm':