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