Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 1 | """Recognize image file formats based on their first few bytes.""" |
Guido van Rossum | 1c34fc7 | 1992-05-27 14:06:59 +0000 | [diff] [blame] | 2 | |
Skip Montanaro | 17ab123 | 2001-01-24 06:27:27 +0000 | [diff] [blame] | 3 | __all__ = ["what"] |
Guido van Rossum | 1c34fc7 | 1992-05-27 14:06:59 +0000 | [diff] [blame] | 4 | |
| 5 | #-------------------------# |
Guido van Rossum | 45ac47c | 1997-10-08 15:22:32 +0000 | [diff] [blame] | 6 | # Recognize image headers # |
Guido van Rossum | 1c34fc7 | 1992-05-27 14:06:59 +0000 | [diff] [blame] | 7 | #-------------------------# |
| 8 | |
Guido van Rossum | 45ac47c | 1997-10-08 15:22:32 +0000 | [diff] [blame] | 9 | def what(file, h=None): |
Serhiy Storchaka | 91b0bc2 | 2014-01-25 19:43:02 +0200 | [diff] [blame] | 10 | f = None |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 11 | try: |
Serhiy Storchaka | 91b0bc2 | 2014-01-25 19:43:02 +0200 | [diff] [blame] | 12 | if h is None: |
| 13 | if isinstance(file, str): |
| 14 | f = open(file, 'rb') |
| 15 | h = f.read(32) |
| 16 | else: |
| 17 | location = file.tell() |
| 18 | h = file.read(32) |
| 19 | file.seek(location) |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 20 | for tf in tests: |
| 21 | res = tf(h, f) |
| 22 | if res: |
| 23 | return res |
| 24 | finally: |
| 25 | if f: f.close() |
| 26 | return None |
Guido van Rossum | 1c34fc7 | 1992-05-27 14:06:59 +0000 | [diff] [blame] | 27 | |
| 28 | |
| 29 | #---------------------------------# |
| 30 | # Subroutines per image file type # |
| 31 | #---------------------------------# |
| 32 | |
| 33 | tests = [] |
| 34 | |
Benjamin Peterson | 0b95290 | 2008-08-16 16:48:16 +0000 | [diff] [blame] | 35 | def test_jpeg(h, f): |
| 36 | """JPEG data in JFIF or Exif format""" |
| 37 | if h[6:10] in (b'JFIF', b'Exif'): |
| 38 | return 'jpeg' |
Guido van Rossum | 1c34fc7 | 1992-05-27 14:06:59 +0000 | [diff] [blame] | 39 | |
Benjamin Peterson | 0b95290 | 2008-08-16 16:48:16 +0000 | [diff] [blame] | 40 | tests.append(test_jpeg) |
| 41 | |
| 42 | def test_png(h, f): |
| 43 | if h.startswith(b'\211PNG\r\n\032\n'): |
| 44 | return 'png' |
| 45 | |
| 46 | tests.append(test_png) |
Guido van Rossum | 1c34fc7 | 1992-05-27 14:06:59 +0000 | [diff] [blame] | 47 | |
| 48 | def test_gif(h, f): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 49 | """GIF ('87 and '89 variants)""" |
Barry Warsaw | 72937f3 | 2007-08-12 14:37:20 +0000 | [diff] [blame] | 50 | if h[:6] in (b'GIF87a', b'GIF89a'): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 51 | return 'gif' |
Guido van Rossum | 1c34fc7 | 1992-05-27 14:06:59 +0000 | [diff] [blame] | 52 | |
| 53 | tests.append(test_gif) |
| 54 | |
Benjamin Peterson | 0b95290 | 2008-08-16 16:48:16 +0000 | [diff] [blame] | 55 | def test_tiff(h, f): |
| 56 | """TIFF (can be in Motorola or Intel byte order)""" |
| 57 | if h[:2] in (b'MM', b'II'): |
| 58 | return 'tiff' |
| 59 | |
| 60 | tests.append(test_tiff) |
| 61 | |
| 62 | def test_rgb(h, f): |
| 63 | """SGI image library""" |
| 64 | if h.startswith(b'\001\332'): |
| 65 | return 'rgb' |
| 66 | |
| 67 | tests.append(test_rgb) |
| 68 | |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 69 | def test_pbm(h, f): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 70 | """PBM (portable bitmap)""" |
| 71 | if len(h) >= 3 and \ |
Guido van Rossum | c934128 | 2007-08-13 17:50:00 +0000 | [diff] [blame] | 72 | h[0] == ord(b'P') and h[1] in b'14' and h[2] in b' \t\n\r': |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 73 | return 'pbm' |
Guido van Rossum | 1c34fc7 | 1992-05-27 14:06:59 +0000 | [diff] [blame] | 74 | |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 75 | tests.append(test_pbm) |
| 76 | |
| 77 | def test_pgm(h, f): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 78 | """PGM (portable graymap)""" |
| 79 | if len(h) >= 3 and \ |
Guido van Rossum | c934128 | 2007-08-13 17:50:00 +0000 | [diff] [blame] | 80 | h[0] == ord(b'P') and h[1] in b'25' and h[2] in b' \t\n\r': |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 81 | return 'pgm' |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 82 | |
| 83 | tests.append(test_pgm) |
| 84 | |
| 85 | def test_ppm(h, f): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 86 | """PPM (portable pixmap)""" |
| 87 | if len(h) >= 3 and \ |
Guido van Rossum | c934128 | 2007-08-13 17:50:00 +0000 | [diff] [blame] | 88 | h[0] == ord(b'P') and h[1] in b'36' and h[2] in b' \t\n\r': |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 89 | return 'ppm' |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 90 | |
| 91 | tests.append(test_ppm) |
Guido van Rossum | 1c34fc7 | 1992-05-27 14:06:59 +0000 | [diff] [blame] | 92 | |
Guido van Rossum | 1c34fc7 | 1992-05-27 14:06:59 +0000 | [diff] [blame] | 93 | def test_rast(h, f): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 94 | """Sun raster file""" |
Guido van Rossum | c934128 | 2007-08-13 17:50:00 +0000 | [diff] [blame] | 95 | if h.startswith(b'\x59\xA6\x6A\x95'): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 96 | return 'rast' |
Guido van Rossum | 1c34fc7 | 1992-05-27 14:06:59 +0000 | [diff] [blame] | 97 | |
| 98 | tests.append(test_rast) |
| 99 | |
Guido van Rossum | 05b55e7 | 1992-06-03 16:48:44 +0000 | [diff] [blame] | 100 | def test_xbm(h, f): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 101 | """X bitmap (X10 or X11)""" |
Guido van Rossum | c934128 | 2007-08-13 17:50:00 +0000 | [diff] [blame] | 102 | if h.startswith(b'#define '): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 103 | return 'xbm' |
Guido van Rossum | 05b55e7 | 1992-06-03 16:48:44 +0000 | [diff] [blame] | 104 | |
| 105 | tests.append(test_xbm) |
| 106 | |
Guido van Rossum | 9e33074 | 1997-04-11 18:59:38 +0000 | [diff] [blame] | 107 | def test_bmp(h, f): |
Guido van Rossum | c934128 | 2007-08-13 17:50:00 +0000 | [diff] [blame] | 108 | if h.startswith(b'BM'): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 109 | return 'bmp' |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 110 | |
Guido van Rossum | 9e33074 | 1997-04-11 18:59:38 +0000 | [diff] [blame] | 111 | tests.append(test_bmp) |
| 112 | |
Serhiy Storchaka | 2f8dca7 | 2014-05-25 11:45:37 +0300 | [diff] [blame] | 113 | def test_webp(h, f): |
| 114 | if h.startswith(b'RIFF') and h[8:12] == b'WEBP': |
| 115 | return 'webp' |
| 116 | |
| 117 | tests.append(test_webp) |
| 118 | |
R David Murray | 2f60820 | 2014-06-26 12:27:57 -0400 | [diff] [blame] | 119 | def test_exr(h, f): |
| 120 | if h.startswith(b'\x76\x2f\x31\x01'): |
| 121 | return 'exr' |
| 122 | |
| 123 | tests.append(test_exr) |
| 124 | |
Guido van Rossum | 1c34fc7 | 1992-05-27 14:06:59 +0000 | [diff] [blame] | 125 | #--------------------# |
| 126 | # Small test program # |
| 127 | #--------------------# |
| 128 | |
| 129 | def test(): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 130 | import sys |
| 131 | recursive = 0 |
| 132 | if sys.argv[1:] and sys.argv[1] == '-r': |
| 133 | del sys.argv[1:2] |
| 134 | recursive = 1 |
| 135 | try: |
| 136 | if sys.argv[1:]: |
| 137 | testall(sys.argv[1:], recursive, 1) |
| 138 | else: |
| 139 | testall(['.'], recursive, 1) |
| 140 | except KeyboardInterrupt: |
| 141 | sys.stderr.write('\n[Interrupted]\n') |
| 142 | sys.exit(1) |
Guido van Rossum | 1c34fc7 | 1992-05-27 14:06:59 +0000 | [diff] [blame] | 143 | |
| 144 | def testall(list, recursive, toplevel): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 145 | import sys |
| 146 | import os |
| 147 | for filename in list: |
| 148 | if os.path.isdir(filename): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 149 | print(filename + '/:', end=' ') |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 150 | if recursive or toplevel: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 151 | print('recursing down:') |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 152 | import glob |
| 153 | names = glob.glob(os.path.join(filename, '*')) |
| 154 | testall(names, recursive, 0) |
| 155 | else: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 156 | print('*** directory (use -r) ***') |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 157 | else: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 158 | print(filename + ':', end=' ') |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 159 | sys.stdout.flush() |
| 160 | try: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 161 | print(what(filename)) |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 162 | except OSError: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 163 | print('*** not found ***') |
Barry Warsaw | 72937f3 | 2007-08-12 14:37:20 +0000 | [diff] [blame] | 164 | |
| 165 | if __name__ == '__main__': |
| 166 | test() |