annotate Lib/imghdr.py @ 5817:48b94de85bf3 legacy-trunk

Extensive changes by AMK.
author Guido van Rossum <guido@python.org>
date Thu, 17 Jul 1997 16:29:42 +0000
parents 4459172f40bb
children 77a22d49dd85
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
876
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 # Recognizing image files based on their first few bytes.
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 #-------------------------#
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 # Recognize sound headers #
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 #-------------------------#
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
7
3821
240f834aee8b Added optional second arg to what(), giving the data read from the file
Guido van Rossum <guido@python.org>
parents: 1316
diff changeset
8 def what(filename, h=None):
4108
6d9d5e7e588c imghdr.what(): check for not h'. This lets
Guido van Rossum <guido@python.org>
parents: 3821
diff changeset
9 if h is None:
3821
240f834aee8b Added optional second arg to what(), giving the data read from the file
Guido van Rossum <guido@python.org>
parents: 1316
diff changeset
10 f = open(filename, 'r')
240f834aee8b Added optional second arg to what(), giving the data read from the file
Guido van Rossum <guido@python.org>
parents: 1316
diff changeset
11 h = f.read(32)
240f834aee8b Added optional second arg to what(), giving the data read from the file
Guido van Rossum <guido@python.org>
parents: 1316
diff changeset
12 else:
240f834aee8b Added optional second arg to what(), giving the data read from the file
Guido van Rossum <guido@python.org>
parents: 1316
diff changeset
13 f = None
240f834aee8b Added optional second arg to what(), giving the data read from the file
Guido van Rossum <guido@python.org>
parents: 1316
diff changeset
14 try:
240f834aee8b Added optional second arg to what(), giving the data read from the file
Guido van Rossum <guido@python.org>
parents: 1316
diff changeset
15 for tf in tests:
240f834aee8b Added optional second arg to what(), giving the data read from the file
Guido van Rossum <guido@python.org>
parents: 1316
diff changeset
16 res = tf(h, f)
240f834aee8b Added optional second arg to what(), giving the data read from the file
Guido van Rossum <guido@python.org>
parents: 1316
diff changeset
17 if res:
240f834aee8b Added optional second arg to what(), giving the data read from the file
Guido van Rossum <guido@python.org>
parents: 1316
diff changeset
18 return res
240f834aee8b Added optional second arg to what(), giving the data read from the file
Guido van Rossum <guido@python.org>
parents: 1316
diff changeset
19 finally:
240f834aee8b Added optional second arg to what(), giving the data read from the file
Guido van Rossum <guido@python.org>
parents: 1316
diff changeset
20 if f: f.close()
876
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 return None
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
22
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
23
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 #---------------------------------#
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 # Subroutines per image file type #
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 #---------------------------------#
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
27
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 tests = []
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
29
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 def test_rgb(h, f):
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 # SGI image library
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 if h[:2] == '\001\332':
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 return 'rgb'
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
34
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 tests.append(test_rgb)
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
36
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 def test_gif(h, f):
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 # GIF ('87 and '89 variants)
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 if h[:6] in ('GIF87a', 'GIF89a'):
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 return 'gif'
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
41
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 tests.append(test_gif)
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
43
1078
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 880
diff changeset
44 def test_pbm(h, f):
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 880
diff changeset
45 # PBM (portable bitmap)
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 880
diff changeset
46 if len(h) >= 3 and \
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 880
diff changeset
47 h[0] == 'P' and h[1] in '14' and h[2] in ' \t\n\r':
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 880
diff changeset
48 return 'pbm'
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 880
diff changeset
49
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 880
diff changeset
50 tests.append(test_pbm)
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 880
diff changeset
51
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 880
diff changeset
52 def test_pgm(h, f):
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 880
diff changeset
53 # PGM (portable graymap)
880
36b934326fed Fix pnm test for short files and add X bitmap as recognized type
Guido van Rossum <guido@python.org>
parents: 876
diff changeset
54 if len(h) >= 3 and \
1078
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 880
diff changeset
55 h[0] == 'P' and h[1] in '25' and h[2] in ' \t\n\r':
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 880
diff changeset
56 return 'pgm'
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 880
diff changeset
57
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 880
diff changeset
58 tests.append(test_pgm)
876
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
59
1078
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 880
diff changeset
60 def test_ppm(h, f):
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 880
diff changeset
61 # PPM (portable pixmap)
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 880
diff changeset
62 if len(h) >= 3 and \
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 880
diff changeset
63 h[0] == 'P' and h[1] in '36' and h[2] in ' \t\n\r':
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 880
diff changeset
64 return 'ppm'
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 880
diff changeset
65
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 880
diff changeset
66 tests.append(test_ppm)
876
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
67
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 def test_tiff(h, f):
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 # TIFF (can be in Motorola or Intel byte order)
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 if h[:2] in ('MM', 'II'):
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 return 'tiff'
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
72
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 tests.append(test_tiff)
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
74
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 def test_rast(h, f):
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 # Sun raster file
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 if h[:4] == '\x59\xA6\x6A\x95':
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 return 'rast'
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
79
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 tests.append(test_rast)
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
81
880
36b934326fed Fix pnm test for short files and add X bitmap as recognized type
Guido van Rossum <guido@python.org>
parents: 876
diff changeset
82 def test_xbm(h, f):
36b934326fed Fix pnm test for short files and add X bitmap as recognized type
Guido van Rossum <guido@python.org>
parents: 876
diff changeset
83 # X bitmap (X10 or X11)
36b934326fed Fix pnm test for short files and add X bitmap as recognized type
Guido van Rossum <guido@python.org>
parents: 876
diff changeset
84 s = '#define '
36b934326fed Fix pnm test for short files and add X bitmap as recognized type
Guido van Rossum <guido@python.org>
parents: 876
diff changeset
85 if h[:len(s)] == s:
36b934326fed Fix pnm test for short files and add X bitmap as recognized type
Guido van Rossum <guido@python.org>
parents: 876
diff changeset
86 return 'xbm'
36b934326fed Fix pnm test for short files and add X bitmap as recognized type
Guido van Rossum <guido@python.org>
parents: 876
diff changeset
87
36b934326fed Fix pnm test for short files and add X bitmap as recognized type
Guido van Rossum <guido@python.org>
parents: 876
diff changeset
88 tests.append(test_xbm)
36b934326fed Fix pnm test for short files and add X bitmap as recognized type
Guido van Rossum <guido@python.org>
parents: 876
diff changeset
89
1316
2f3f293ef5f1 * calendar.py: all libC functionality now moved to built-in time module
Guido van Rossum <guido@python.org>
parents: 1078
diff changeset
90 def test_jpeg(h, f):
2f3f293ef5f1 * calendar.py: all libC functionality now moved to built-in time module
Guido van Rossum <guido@python.org>
parents: 1078
diff changeset
91 # JPEG data in JFIF format
2f3f293ef5f1 * calendar.py: all libC functionality now moved to built-in time module
Guido van Rossum <guido@python.org>
parents: 1078
diff changeset
92 if h[6:10] == 'JFIF':
2f3f293ef5f1 * calendar.py: all libC functionality now moved to built-in time module
Guido van Rossum <guido@python.org>
parents: 1078
diff changeset
93 return 'jpeg'
2f3f293ef5f1 * calendar.py: all libC functionality now moved to built-in time module
Guido van Rossum <guido@python.org>
parents: 1078
diff changeset
94
2f3f293ef5f1 * calendar.py: all libC functionality now moved to built-in time module
Guido van Rossum <guido@python.org>
parents: 1078
diff changeset
95 tests.append(test_jpeg)
876
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
96
5304
4459172f40bb (Jack:) Recognize PNG files.
Guido van Rossum <guido@python.org>
parents: 4108
diff changeset
97 def test_bmp(h, f):
4459172f40bb (Jack:) Recognize PNG files.
Guido van Rossum <guido@python.org>
parents: 4108
diff changeset
98 if h[:2] == 'BM':
4459172f40bb (Jack:) Recognize PNG files.
Guido van Rossum <guido@python.org>
parents: 4108
diff changeset
99 return 'bmp'
4459172f40bb (Jack:) Recognize PNG files.
Guido van Rossum <guido@python.org>
parents: 4108
diff changeset
100
4459172f40bb (Jack:) Recognize PNG files.
Guido van Rossum <guido@python.org>
parents: 4108
diff changeset
101 tests.append(test_bmp)
4459172f40bb (Jack:) Recognize PNG files.
Guido van Rossum <guido@python.org>
parents: 4108
diff changeset
102
4459172f40bb (Jack:) Recognize PNG files.
Guido van Rossum <guido@python.org>
parents: 4108
diff changeset
103 def test_png(h, f):
4459172f40bb (Jack:) Recognize PNG files.
Guido van Rossum <guido@python.org>
parents: 4108
diff changeset
104 if h[:8] == "\211PNG\r\n\032\n":
4459172f40bb (Jack:) Recognize PNG files.
Guido van Rossum <guido@python.org>
parents: 4108
diff changeset
105 return 'png'
4459172f40bb (Jack:) Recognize PNG files.
Guido van Rossum <guido@python.org>
parents: 4108
diff changeset
106
4459172f40bb (Jack:) Recognize PNG files.
Guido van Rossum <guido@python.org>
parents: 4108
diff changeset
107 tests.append(test_png)
4459172f40bb (Jack:) Recognize PNG files.
Guido van Rossum <guido@python.org>
parents: 4108
diff changeset
108
876
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 #--------------------#
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 # Small test program #
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 #--------------------#
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
112
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 def test():
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 import sys
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 recursive = 0
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 if sys.argv[1:] and sys.argv[1] == '-r':
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 del sys.argv[1:2]
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 recursive = 1
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 try:
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 if sys.argv[1:]:
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 testall(sys.argv[1:], recursive, 1)
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 else:
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 testall(['.'], recursive, 1)
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 except KeyboardInterrupt:
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 sys.stderr.write('\n[Interrupted]\n')
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 sys.exit(1)
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
127
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 def testall(list, recursive, toplevel):
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 import sys
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 import os
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 for filename in list:
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 if os.path.isdir(filename):
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 print filename + '/:',
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 if recursive or toplevel:
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 print 'recursing down:'
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 import glob
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 names = glob.glob(os.path.join(filename, '*'))
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 testall(names, recursive, 0)
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 else:
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 print '*** directory (use -r) ***'
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 else:
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 print filename + ':',
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 sys.stdout.flush()
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 try:
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 print what(filename)
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 except IOError:
ec31748837dc Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 print '*** not found ***'