blob: 063bfe82ce55753bc20bee0140dba8a819981641 [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 Rossum1c34fc71992-05-27 14:06:59 +000084
85#--------------------#
86# Small test program #
87#--------------------#
88
89def test():
90 import sys
91 recursive = 0
92 if sys.argv[1:] and sys.argv[1] == '-r':
93 del sys.argv[1:2]
94 recursive = 1
95 try:
96 if sys.argv[1:]:
97 testall(sys.argv[1:], recursive, 1)
98 else:
99 testall(['.'], recursive, 1)
100 except KeyboardInterrupt:
101 sys.stderr.write('\n[Interrupted]\n')
102 sys.exit(1)
103
104def testall(list, recursive, toplevel):
105 import sys
106 import os
107 for filename in list:
108 if os.path.isdir(filename):
109 print filename + '/:',
110 if recursive or toplevel:
111 print 'recursing down:'
112 import glob
113 names = glob.glob(os.path.join(filename, '*'))
114 testall(names, recursive, 0)
115 else:
116 print '*** directory (use -r) ***'
117 else:
118 print filename + ':',
119 sys.stdout.flush()
120 try:
121 print what(filename)
122 except IOError:
123 print '*** not found ***'