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