1 # Recognizing image files based on their first few bytes.
4 #-------------------------#
5 # Recognize image headers #
6 #-------------------------#
8 def what(file, h
=None):
10 if type(file) == type(''):
14 location
= file.tell()
30 #---------------------------------#
31 # Subroutines per image file type #
32 #---------------------------------#
38 if h
[:2] == '\001\332':
41 tests
.append(test_rgb
)
44 # GIF ('87 and '89 variants)
45 if h
[:6] in ('GIF87a', 'GIF89a'):
48 tests
.append(test_gif
)
51 # PBM (portable bitmap)
53 h
[0] == 'P' and h
[1] in '14' and h
[2] in ' \t\n\r':
56 tests
.append(test_pbm
)
59 # PGM (portable graymap)
61 h
[0] == 'P' and h
[1] in '25' and h
[2] in ' \t\n\r':
64 tests
.append(test_pgm
)
67 # PPM (portable pixmap)
69 h
[0] == 'P' and h
[1] in '36' and h
[2] in ' \t\n\r':
72 tests
.append(test_ppm
)
75 # TIFF (can be in Motorola or Intel byte order)
76 if h
[:2] in ('MM', 'II'):
79 tests
.append(test_tiff
)
83 if h
[:4] == '\x59\xA6\x6A\x95':
86 tests
.append(test_rast
)
89 # X bitmap (X10 or X11)
94 tests
.append(test_xbm
)
97 # JPEG data in JFIF format
101 tests
.append(test_jpeg
)
107 tests
.append(test_bmp
)
110 if h
[:8] == "\211PNG\r\n\032\n":
113 tests
.append(test_png
)
115 #--------------------#
116 # Small test program #
117 #--------------------#
122 if sys
.argv
[1:] and sys
.argv
[1] == '-r':
127 testall(sys
.argv
[1:], recursive
, 1)
129 testall(['.'], recursive
, 1)
130 except KeyboardInterrupt:
131 sys
.stderr
.write('\n[Interrupted]\n')
134 def testall(list, recursive
, toplevel
):
137 for filename
in list:
138 if os
.path
.isdir(filename
):
139 print filename
+ '/:',
140 if recursive
or toplevel
:
141 print 'recursing down:'
143 names
= glob
.glob(os
.path
.join(filename
, '*'))
144 testall(names
, recursive
, 0)
146 print '*** directory (use -r) ***'
148 print filename
+ ':',
153 print '*** not found ***'