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)
108
tests.append(test_bmp)
111
if h[:8] == "\211PNG\r\n\032\n":
114
tests.append(test_png)
116
#--------------------#
117
# Small test program #
118
#--------------------#
123
if sys.argv[1:] and sys.argv[1] == '-r':
128
testall(sys.argv[1:], recursive, 1)
130
testall(['.'], recursive, 1)
131
except KeyboardInterrupt:
132
sys.stderr.write('\n[Interrupted]\n')
135
def testall(list, recursive, toplevel):
138
for filename in list:
139
if os.path.isdir(filename):
140
print filename + '/:',
141
if recursive or toplevel:
142
print 'recursing down:'
144
names = glob.glob(os.path.join(filename, '*'))
145
testall(names, recursive, 0)
147
print '*** directory (use -r) ***'
149
print filename + ':',
154
print '*** not found ***'