Guido van Rossum | 1c34fc7 | 1992-05-27 14:06:59 +0000 | [diff] [blame] | 1 | # Recognizing image files based on their first few bytes. |
| 2 | |
| 3 | |
| 4 | #-------------------------# |
Guido van Rossum | 45ac47c | 1997-10-08 15:22:32 +0000 | [diff] [blame] | 5 | # Recognize image headers # |
Guido van Rossum | 1c34fc7 | 1992-05-27 14:06:59 +0000 | [diff] [blame] | 6 | #-------------------------# |
| 7 | |
Guido van Rossum | 45ac47c | 1997-10-08 15:22:32 +0000 | [diff] [blame] | 8 | def what(file, h=None): |
Guido van Rossum | f813f56 | 1996-08-22 21:20:46 +0000 | [diff] [blame] | 9 | if h is None: |
Guido van Rossum | 45ac47c | 1997-10-08 15:22:32 +0000 | [diff] [blame] | 10 | if type(file) == type(''): |
| 11 | f = open(file, 'rb') |
| 12 | h = f.read(32) |
| 13 | else: |
| 14 | location = file.tell() |
| 15 | h = file.read(32) |
| 16 | file.seek(location) |
| 17 | f = None |
Guido van Rossum | 81749b0 | 1996-07-30 16:26:42 +0000 | [diff] [blame] | 18 | else: |
| 19 | f = None |
| 20 | try: |
| 21 | for tf in tests: |
| 22 | res = tf(h, f) |
| 23 | if res: |
| 24 | return res |
| 25 | finally: |
| 26 | if f: f.close() |
Guido van Rossum | 1c34fc7 | 1992-05-27 14:06:59 +0000 | [diff] [blame] | 27 | return None |
| 28 | |
| 29 | |
| 30 | #---------------------------------# |
| 31 | # Subroutines per image file type # |
| 32 | #---------------------------------# |
| 33 | |
| 34 | tests = [] |
| 35 | |
| 36 | def test_rgb(h, f): |
| 37 | # SGI image library |
| 38 | if h[:2] == '\001\332': |
| 39 | return 'rgb' |
| 40 | |
| 41 | tests.append(test_rgb) |
| 42 | |
| 43 | def test_gif(h, f): |
| 44 | # GIF ('87 and '89 variants) |
| 45 | if h[:6] in ('GIF87a', 'GIF89a'): |
| 46 | return 'gif' |
| 47 | |
| 48 | tests.append(test_gif) |
| 49 | |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 50 | def test_pbm(h, f): |
| 51 | # PBM (portable bitmap) |
Guido van Rossum | 05b55e7 | 1992-06-03 16:48:44 +0000 | [diff] [blame] | 52 | if len(h) >= 3 and \ |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 53 | h[0] == 'P' and h[1] in '14' and h[2] in ' \t\n\r': |
| 54 | return 'pbm' |
Guido van Rossum | 1c34fc7 | 1992-05-27 14:06:59 +0000 | [diff] [blame] | 55 | |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 56 | tests.append(test_pbm) |
| 57 | |
| 58 | def test_pgm(h, f): |
| 59 | # PGM (portable graymap) |
| 60 | if len(h) >= 3 and \ |
| 61 | h[0] == 'P' and h[1] in '25' and h[2] in ' \t\n\r': |
| 62 | return 'pgm' |
| 63 | |
| 64 | tests.append(test_pgm) |
| 65 | |
| 66 | def test_ppm(h, f): |
| 67 | # PPM (portable pixmap) |
| 68 | if len(h) >= 3 and \ |
| 69 | h[0] == 'P' and h[1] in '36' and h[2] in ' \t\n\r': |
| 70 | return 'ppm' |
| 71 | |
| 72 | tests.append(test_ppm) |
Guido van Rossum | 1c34fc7 | 1992-05-27 14:06:59 +0000 | [diff] [blame] | 73 | |
| 74 | def test_tiff(h, f): |
| 75 | # TIFF (can be in Motorola or Intel byte order) |
| 76 | if h[:2] in ('MM', 'II'): |
| 77 | return 'tiff' |
| 78 | |
| 79 | tests.append(test_tiff) |
| 80 | |
| 81 | def test_rast(h, f): |
| 82 | # Sun raster file |
| 83 | if h[:4] == '\x59\xA6\x6A\x95': |
| 84 | return 'rast' |
| 85 | |
| 86 | tests.append(test_rast) |
| 87 | |
Guido van Rossum | 05b55e7 | 1992-06-03 16:48:44 +0000 | [diff] [blame] | 88 | def test_xbm(h, f): |
| 89 | # X bitmap (X10 or X11) |
| 90 | s = '#define ' |
| 91 | if h[:len(s)] == s: |
| 92 | return 'xbm' |
| 93 | |
| 94 | tests.append(test_xbm) |
| 95 | |
Guido van Rossum | 5cfa5df | 1993-06-23 09:30:50 +0000 | [diff] [blame] | 96 | def test_jpeg(h, f): |
| 97 | # JPEG data in JFIF format |
| 98 | if h[6:10] == 'JFIF': |
| 99 | return 'jpeg' |
| 100 | |
| 101 | tests.append(test_jpeg) |
Guido van Rossum | 1c34fc7 | 1992-05-27 14:06:59 +0000 | [diff] [blame] | 102 | |
Guido van Rossum | 9e33074 | 1997-04-11 18:59:38 +0000 | [diff] [blame] | 103 | def test_bmp(h, f): |
| 104 | if h[:2] == 'BM': |
| 105 | return 'bmp' |
| 106 | |
| 107 | tests.append(test_bmp) |
| 108 | |
| 109 | def test_png(h, f): |
| 110 | if h[:8] == "\211PNG\r\n\032\n": |
| 111 | return 'png' |
| 112 | |
| 113 | tests.append(test_png) |
| 114 | |
Guido van Rossum | 1c34fc7 | 1992-05-27 14:06:59 +0000 | [diff] [blame] | 115 | #--------------------# |
| 116 | # Small test program # |
| 117 | #--------------------# |
| 118 | |
| 119 | def test(): |
| 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) |
| 133 | |
| 134 | def testall(list, recursive, toplevel): |
| 135 | import sys |
| 136 | import os |
| 137 | for filename in list: |
| 138 | if os.path.isdir(filename): |
| 139 | print filename + '/:', |
| 140 | if recursive or toplevel: |
| 141 | print 'recursing down:' |
| 142 | import glob |
| 143 | names = glob.glob(os.path.join(filename, '*')) |
| 144 | testall(names, recursive, 0) |
| 145 | else: |
| 146 | print '*** directory (use -r) ***' |
| 147 | else: |
| 148 | print filename + ':', |
| 149 | sys.stdout.flush() |
| 150 | try: |
| 151 | print what(filename) |
| 152 | except IOError: |
| 153 | print '*** not found ***' |