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