1 # Recognizing image files based on their first few bytes.
4 #-------------------------#
5 # Recognize sound headers #
6 #-------------------------#
9 f
= open(filename
, 'r')
18 #---------------------------------#
19 # Subroutines per image file type #
20 #---------------------------------#
26 if h
[:2] == '\001\332':
29 tests
.append(test_rgb
)
32 # GIF ('87 and '89 variants)
33 if h
[:6] in ('GIF87a', 'GIF89a'):
36 tests
.append(test_gif
)
39 # PBM (portable bitmap)
41 h
[0] == 'P' and h
[1] in '14' and h
[2] in ' \t\n\r':
44 tests
.append(test_pbm
)
47 # PGM (portable graymap)
49 h
[0] == 'P' and h
[1] in '25' and h
[2] in ' \t\n\r':
52 tests
.append(test_pgm
)
55 # PPM (portable pixmap)
57 h
[0] == 'P' and h
[1] in '36' and h
[2] in ' \t\n\r':
60 tests
.append(test_ppm
)
63 # TIFF (can be in Motorola or Intel byte order)
64 if h
[:2] in ('MM', 'II'):
67 tests
.append(test_tiff
)
71 if h
[:4] == '\x59\xA6\x6A\x95':
74 tests
.append(test_rast
)
77 # X bitmap (X10 or X11)
82 tests
.append(test_xbm
)
85 # JPEG data in JFIF format
89 tests
.append(test_jpeg
)
91 #--------------------#
92 # Small test program #
93 #--------------------#
98 if sys
.argv
[1:] and sys
.argv
[1] == '-r':
103 testall(sys
.argv
[1:], recursive
, 1)
105 testall(['.'], recursive
, 1)
106 except KeyboardInterrupt:
107 sys
.stderr
.write('\n[Interrupted]\n')
110 def testall(list, recursive
, toplevel
):
113 for filename
in list:
114 if os
.path
.isdir(filename
):
115 print filename
+ '/:',
116 if recursive
or toplevel
:
117 print 'recursing down:'
119 names
= glob
.glob(os
.path
.join(filename
, '*'))
120 testall(names
, recursive
, 0)
122 print '*** directory (use -r) ***'
124 print filename
+ ':',
129 print '*** not found ***'