blob: 422471fb4467a1416ce7aa687a69f6d260426f1a [file] [log] [blame]
Guido van Rossum1c34fc71992-05-27 14:06:59 +00001# Recognizing image files based on their first few bytes.
2
3
4#-------------------------#
Guido van Rossum45ac47c1997-10-08 15:22:32 +00005# Recognize image headers #
Guido van Rossum1c34fc71992-05-27 14:06:59 +00006#-------------------------#
7
Guido van Rossum45ac47c1997-10-08 15:22:32 +00008def what(file, h=None):
Guido van Rossumf813f561996-08-22 21:20:46 +00009 if h is None:
Guido van Rossum45ac47c1997-10-08 15:22:32 +000010 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 Rossum81749b01996-07-30 16:26:42 +000018 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 Rossum1c34fc71992-05-27 14:06:59 +000027 return None
28
29
30#---------------------------------#
31# Subroutines per image file type #
32#---------------------------------#
33
34tests = []
35
36def test_rgb(h, f):
37 # SGI image library
38 if h[:2] == '\001\332':
39 return 'rgb'
40
41tests.append(test_rgb)
42
43def test_gif(h, f):
44 # GIF ('87 and '89 variants)
45 if h[:6] in ('GIF87a', 'GIF89a'):
46 return 'gif'
47
48tests.append(test_gif)
49
Guido van Rossum2db91351992-10-18 17:09:59 +000050def test_pbm(h, f):
51 # PBM (portable bitmap)
Guido van Rossum05b55e71992-06-03 16:48:44 +000052 if len(h) >= 3 and \
Guido van Rossum2db91351992-10-18 17:09:59 +000053 h[0] == 'P' and h[1] in '14' and h[2] in ' \t\n\r':
54 return 'pbm'
Guido van Rossum1c34fc71992-05-27 14:06:59 +000055
Guido van Rossum2db91351992-10-18 17:09:59 +000056tests.append(test_pbm)
57
58def 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
64tests.append(test_pgm)
65
66def 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
72tests.append(test_ppm)
Guido van Rossum1c34fc71992-05-27 14:06:59 +000073
74def 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
79tests.append(test_tiff)
80
81def test_rast(h, f):
82 # Sun raster file
83 if h[:4] == '\x59\xA6\x6A\x95':
84 return 'rast'
85
86tests.append(test_rast)
87
Guido van Rossum05b55e71992-06-03 16:48:44 +000088def test_xbm(h, f):
89 # X bitmap (X10 or X11)
90 s = '#define '
91 if h[:len(s)] == s:
92 return 'xbm'
93
94tests.append(test_xbm)
95
Guido van Rossum5cfa5df1993-06-23 09:30:50 +000096def test_jpeg(h, f):
97 # JPEG data in JFIF format
98 if h[6:10] == 'JFIF':
99 return 'jpeg'
100
101tests.append(test_jpeg)
Guido van Rossum1c34fc71992-05-27 14:06:59 +0000102
Guido van Rossum9e330741997-04-11 18:59:38 +0000103def test_bmp(h, f):
104 if h[:2] == 'BM':
105 return 'bmp'
106
107tests.append(test_bmp)
108
109def test_png(h, f):
110 if h[:8] == "\211PNG\r\n\032\n":
111 return 'png'
112
113tests.append(test_png)
114
Guido van Rossum1c34fc71992-05-27 14:06:59 +0000115#--------------------#
116# Small test program #
117#--------------------#
118
119def 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
134def 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 ***'