Added 'description' class attribute to every command class (to help the
[python/dscho.git] / Lib / imghdr.py
blob422471fb4467a1416ce7aa687a69f6d260426f1a
1 # Recognizing image files based on their first few bytes.
4 #-------------------------#
5 # Recognize image headers #
6 #-------------------------#
8 def what(file, h=None):
9 if h is None:
10 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
18 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()
27 return None
30 #---------------------------------#
31 # Subroutines per image file type #
32 #---------------------------------#
34 tests = []
36 def test_rgb(h, f):
37 # SGI image library
38 if h[:2] == '\001\332':
39 return 'rgb'
41 tests.append(test_rgb)
43 def test_gif(h, f):
44 # GIF ('87 and '89 variants)
45 if h[:6] in ('GIF87a', 'GIF89a'):
46 return 'gif'
48 tests.append(test_gif)
50 def test_pbm(h, f):
51 # PBM (portable bitmap)
52 if len(h) >= 3 and \
53 h[0] == 'P' and h[1] in '14' and h[2] in ' \t\n\r':
54 return 'pbm'
56 tests.append(test_pbm)
58 def 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'
64 tests.append(test_pgm)
66 def 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'
72 tests.append(test_ppm)
74 def test_tiff(h, f):
75 # TIFF (can be in Motorola or Intel byte order)
76 if h[:2] in ('MM', 'II'):
77 return 'tiff'
79 tests.append(test_tiff)
81 def test_rast(h, f):
82 # Sun raster file
83 if h[:4] == '\x59\xA6\x6A\x95':
84 return 'rast'
86 tests.append(test_rast)
88 def test_xbm(h, f):
89 # X bitmap (X10 or X11)
90 s = '#define '
91 if h[:len(s)] == s:
92 return 'xbm'
94 tests.append(test_xbm)
96 def test_jpeg(h, f):
97 # JPEG data in JFIF format
98 if h[6:10] == 'JFIF':
99 return 'jpeg'
101 tests.append(test_jpeg)
103 def test_bmp(h, f):
104 if h[:2] == 'BM':
105 return 'bmp'
107 tests.append(test_bmp)
109 def test_png(h, f):
110 if h[:8] == "\211PNG\r\n\032\n":
111 return 'png'
113 tests.append(test_png)
115 #--------------------#
116 # Small test program #
117 #--------------------#
119 def 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)
134 def 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 ***'