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 type(file) == type(''):
15 location
= file.tell()
31 #---------------------------------#
32 # Subroutines per image file type #
33 #---------------------------------#
38 """SGI image library"""
39 if h
[:2] == '\001\332':
42 tests
.append(test_rgb
)
45 """GIF ('87 and '89 variants)"""
46 if h
[:6] in ('GIF87a', 'GIF89a'):
49 tests
.append(test_gif
)
52 """PBM (portable bitmap)"""
54 h
[0] == 'P' and h
[1] in '14' and h
[2] in ' \t\n\r':
57 tests
.append(test_pbm
)
60 """PGM (portable graymap)"""
62 h
[0] == 'P' and h
[1] in '25' and h
[2] in ' \t\n\r':
65 tests
.append(test_pgm
)
68 """PPM (portable pixmap)"""
70 h
[0] == 'P' and h
[1] in '36' and h
[2] in ' \t\n\r':
73 tests
.append(test_ppm
)
76 """TIFF (can be in Motorola or Intel byte order)"""
77 if h
[:2] in ('MM', 'II'):
80 tests
.append(test_tiff
)
84 if h
[:4] == '\x59\xA6\x6A\x95':
87 tests
.append(test_rast
)
90 """X bitmap (X10 or X11)"""
95 tests
.append(test_xbm
)
98 """JPEG data in JFIF format"""
102 tests
.append(test_jpeg
)
105 """JPEG data in Exif format"""
106 if h
[6:10] == 'Exif':
109 tests
.append(test_exif
)
115 tests
.append(test_bmp
)
118 if h
[:8] == "\211PNG\r\n\032\n":
121 tests
.append(test_png
)
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 ***'