--- imghdr27.py 2016-11-02 16:53:28.027506847 -0400 +++ imghdr_py27.py 2016-11-02 16:36:15.803518448 -0400 @@ -7,18 +7,16 @@ #-------------------------# def what(file, h=None): - if h is None: - if isinstance(file, basestring): - f = open(file, 'rb') - h = f.read(32) - else: - location = file.tell() - h = file.read(32) - file.seek(location) - f = None - else: - f = None + f = None try: + if h is None: + if isinstance(file, basestring): + f = open(file, 'rb') + h = f.read(32) + else: + location = file.tell() + h = file.read(32) + file.seek(location) for tf in tests: res = tf(h, f) if res: @@ -34,13 +32,6 @@ tests = [] -def test_jpeg(h, f): - """JPEG data in JFIF format""" - if h[6:10] == 'JFIF': - return 'jpeg' - -tests.append(test_jpeg) - def test_exif(h, f): """JPEG data in Exif format""" if h[6:10] == 'Exif': @@ -48,6 +39,13 @@ tests.append(test_exif) +def test_jpeg(h, f): + """JPEG data in JFIF format""" + if h[6:10] == 'JFIF': + return 'jpeg' + +tests.append(test_jpeg) + def test_png(h, f): if h[:8] == "\211PNG\r\n\032\n": return 'png' @@ -120,6 +118,23 @@ tests.append(test_bmp) +def test_jpeg1(h, f): + """JPEG data in JFIF format""" + if 'JFIF' in h[:23]: + return 'jpeg' + +tests.append(test_jpeg1) + +JPEG_MARK = bytearray.fromhex('FF D8 FF DB 00 43 00 08 06 06 07 06 \ + 05 08 07 07 07 09 09 08 0A 0C 14 0D 0C 0B 0B 0C 19 12 13 0F') + +def test_jpeg2(h, f): + """JPEG with small header""" + if len(h) >= 32 and 'C' == h[5] and bytearray(h[:32]) == JPEG_MARK: + return 'jpeg' + +tests.append(test_jpeg2) + #--------------------# # Small test program # #--------------------# @@ -159,3 +174,8 @@ print what(filename) except IOError: print '*** not found ***' + + +if __name__ == "__main__": # pragma: no cover + test() +