1 """Recognize image file formats based on their first few bytes."""
5 #-------------------------#
6 # Recognize image headers #
7 #-------------------------#
9 def what(file, h
=None):
11 if isinstance(file, basestring
):
15 location
= file.tell()
31 #---------------------------------#
32 # Subroutines per image file type #
33 #---------------------------------#
38 """JPEG data in JFIF format"""
42 tests
.append(test_jpeg
)
45 """JPEG data in Exif format"""
49 tests
.append(test_exif
)
52 if h
[:8] == "\211PNG\r\n\032\n":
55 tests
.append(test_png
)
58 """GIF ('87 and '89 variants)"""
59 if h
[:6] in ('GIF87a', 'GIF89a'):
62 tests
.append(test_gif
)
65 """TIFF (can be in Motorola or Intel byte order)"""
66 if h
[:2] in ('MM', 'II'):
69 tests
.append(test_tiff
)
72 """SGI image library"""
73 if h
[:2] == '\001\332':
76 tests
.append(test_rgb
)
79 """PBM (portable bitmap)"""
81 h
[0] == 'P' and h
[1] in '14' and h
[2] in ' \t\n\r':
84 tests
.append(test_pbm
)
87 """PGM (portable graymap)"""
89 h
[0] == 'P' and h
[1] in '25' and h
[2] in ' \t\n\r':
92 tests
.append(test_pgm
)
95 """PPM (portable pixmap)"""
97 h
[0] == 'P' and h
[1] in '36' and h
[2] in ' \t\n\r':
100 tests
.append(test_ppm
)
103 """Sun raster file"""
104 if h
[:4] == '\x59\xA6\x6A\x95':
107 tests
.append(test_rast
)
110 """X bitmap (X10 or X11)"""
115 tests
.append(test_xbm
)
121 tests
.append(test_bmp
)
123 #--------------------#
124 # Small test program #
125 #--------------------#
130 if sys
.argv
[1:] and sys
.argv
[1] == '-r':
135 testall(sys
.argv
[1:], recursive
, 1)
137 testall(['.'], recursive
, 1)
138 except KeyboardInterrupt:
139 sys
.stderr
.write('\n[Interrupted]\n')
142 def testall(list, recursive
, toplevel
):
145 for filename
in list:
146 if os
.path
.isdir(filename
):
147 print filename
+ '/:',
148 if recursive
or toplevel
:
149 print 'recursing down:'
151 names
= glob
.glob(os
.path
.join(filename
, '*'))
152 testall(names
, recursive
, 0)
154 print '*** directory (use -r) ***'
156 print filename
+ ':',
161 print '*** not found ***'