[issue16512] imghdr doesn't recognize variant jpeg formats

Martin Vignali report at bugs.python.org
Sat Jun 21 20:44:22 CEST 2014


Martin Vignali added the comment:

I'm okay with just testing the first two bytes, it's the method we currently use for our
internal tools.

But maybe it can be interesting, to add another test, in order to detect incomplete file
(created when a camera make a recording error for example, and very useful to detect, because an incomplete jpeg file, make a crash for a lot of application)

We use this patch of imghdr :

--------------------------------------
def test_jpeg(h, f):
    """JPEG data in JFIF or Exif format"""
    if not h.startswith(b'\xff\xd8'):#Test empty files, and incorrect start of file
        return None
    else:
        if f:#if we test a file, test end of jpeg
            f.seek(-2,2)
            if f.read(2).endswith(b'\xff\xd9'):
                return 'jpeg'
        else:#if we just test the header, consider this is a valid jpeg and not test end of file
            return 'jpeg'
-------------------------------------

----------
nosy: +mvignali

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue16512>
_______________________________________


More information about the Python-bugs-list mailing list