blob: 62518b5f36c9b9400d75364483c1da9dd0650996 [file] [log] [blame]
Guido van Rossum1c34fc71992-05-27 14:06:59 +00001# Recognizing image files based on their first few bytes.
2
3
4#-------------------------#
5# Recognize sound headers #
6#-------------------------#
7
8def 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
22tests = []
23
24def test_rgb(h, f):
25 # SGI image library
26 if h[:2] == '\001\332':
27 return 'rgb'
28
29tests.append(test_rgb)
30
31def test_gif(h, f):
32 # GIF ('87 and '89 variants)
33 if h[:6] in ('GIF87a', 'GIF89a'):
34 return 'gif'
35
36tests.append(test_gif)
37
Guido van Rossum2db91351992-10-18 17:09:59 +000038def test_pbm(h, f):
39 # PBM (portable bitmap)
Guido van Rossum05b55e71992-06-03 16:48:44 +000040 if len(h) >= 3 and \
Guido van Rossum2db91351992-10-18 17:09:59 +000041 h[0] == 'P' and h[1] in '14' and h[2] in ' \t\n\r':
42 return 'pbm'
Guido van Rossum1c34fc71992-05-27 14:06:59 +000043
Guido van Rossum2db91351992-10-18 17:09:59 +000044tests.append(test_pbm)
45
46def 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
52tests.append(test_pgm)
53
54def 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
60tests.append(test_ppm)
Guido van Rossum1c34fc71992-05-27 14:06:59 +000061
62def 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
67tests.append(test_tiff)
68
69def test_rast(h, f):
70 # Sun raster file
71 if h[:4] == '\x59\xA6\x6A\x95':
72 return 'rast'
73
74tests.append(test_rast)
75
Guido van Rossum05b55e71992-06-03 16:48:44 +000076def test_xbm(h, f):
77 # X bitmap (X10 or X11)
78 s = '#define '
79 if h[:len(s)] == s:
80 return 'xbm'
81
82tests.append(test_xbm)
83
Guido van Rossum5cfa5df1993-06-23 09:30:50 +000084def test_jpeg(h, f):
85 # JPEG data in JFIF format
86 if h[6:10] == 'JFIF':
87 return 'jpeg'
88
89tests.append(test_jpeg)
Guido van Rossum1c34fc71992-05-27 14:06:59 +000090
91#--------------------#
92# Small test program #
93#--------------------#
94
95def 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
110def 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 ***'