blob: dc5fb2298014b3593f93cdd8417fbc35a728523b [file] [log] [blame]
Guido van Rossum54f22ed2000-02-04 15:10:34 +00001"""Recognize image file formats based on their first few bytes."""
Guido van Rossum1c34fc71992-05-27 14:06:59 +00002
Skip Montanaro17ab1232001-01-24 06:27:27 +00003__all__ = ["what"]
Guido van Rossum1c34fc71992-05-27 14:06:59 +00004
5#-------------------------#
Guido van Rossum45ac47c1997-10-08 15:22:32 +00006# Recognize image headers #
Guido van Rossum1c34fc71992-05-27 14:06:59 +00007#-------------------------#
8
Guido van Rossum45ac47c1997-10-08 15:22:32 +00009def what(file, h=None):
Guido van Rossum54f22ed2000-02-04 15:10:34 +000010 if h is None:
11 if type(file) == type(''):
12 f = open(file, 'rb')
13 h = f.read(32)
14 else:
15 location = file.tell()
16 h = file.read(32)
17 file.seek(location)
Tim Peters07e99cb2001-01-14 23:47:14 +000018 f = None
Guido van Rossum54f22ed2000-02-04 15:10:34 +000019 else:
20 f = None
21 try:
22 for tf in tests:
23 res = tf(h, f)
24 if res:
25 return res
26 finally:
27 if f: f.close()
28 return None
Guido van Rossum1c34fc71992-05-27 14:06:59 +000029
30
31#---------------------------------#
32# Subroutines per image file type #
33#---------------------------------#
34
35tests = []
36
37def test_rgb(h, f):
Guido van Rossum54f22ed2000-02-04 15:10:34 +000038 """SGI image library"""
39 if h[:2] == '\001\332':
40 return 'rgb'
Guido van Rossum1c34fc71992-05-27 14:06:59 +000041
42tests.append(test_rgb)
43
44def test_gif(h, f):
Guido van Rossum54f22ed2000-02-04 15:10:34 +000045 """GIF ('87 and '89 variants)"""
46 if h[:6] in ('GIF87a', 'GIF89a'):
47 return 'gif'
Guido van Rossum1c34fc71992-05-27 14:06:59 +000048
49tests.append(test_gif)
50
Guido van Rossum2db91351992-10-18 17:09:59 +000051def test_pbm(h, f):
Guido van Rossum54f22ed2000-02-04 15:10:34 +000052 """PBM (portable bitmap)"""
53 if len(h) >= 3 and \
54 h[0] == 'P' and h[1] in '14' and h[2] in ' \t\n\r':
55 return 'pbm'
Guido van Rossum1c34fc71992-05-27 14:06:59 +000056
Guido van Rossum2db91351992-10-18 17:09:59 +000057tests.append(test_pbm)
58
59def test_pgm(h, f):
Guido van Rossum54f22ed2000-02-04 15:10:34 +000060 """PGM (portable graymap)"""
61 if len(h) >= 3 and \
62 h[0] == 'P' and h[1] in '25' and h[2] in ' \t\n\r':
63 return 'pgm'
Guido van Rossum2db91351992-10-18 17:09:59 +000064
65tests.append(test_pgm)
66
67def test_ppm(h, f):
Guido van Rossum54f22ed2000-02-04 15:10:34 +000068 """PPM (portable pixmap)"""
69 if len(h) >= 3 and \
70 h[0] == 'P' and h[1] in '36' and h[2] in ' \t\n\r':
71 return 'ppm'
Guido van Rossum2db91351992-10-18 17:09:59 +000072
73tests.append(test_ppm)
Guido van Rossum1c34fc71992-05-27 14:06:59 +000074
75def test_tiff(h, f):
Guido van Rossum54f22ed2000-02-04 15:10:34 +000076 """TIFF (can be in Motorola or Intel byte order)"""
77 if h[:2] in ('MM', 'II'):
78 return 'tiff'
Guido van Rossum1c34fc71992-05-27 14:06:59 +000079
80tests.append(test_tiff)
81
82def test_rast(h, f):
Guido van Rossum54f22ed2000-02-04 15:10:34 +000083 """Sun raster file"""
84 if h[:4] == '\x59\xA6\x6A\x95':
85 return 'rast'
Guido van Rossum1c34fc71992-05-27 14:06:59 +000086
87tests.append(test_rast)
88
Guido van Rossum05b55e71992-06-03 16:48:44 +000089def test_xbm(h, f):
Guido van Rossum54f22ed2000-02-04 15:10:34 +000090 """X bitmap (X10 or X11)"""
91 s = '#define '
92 if h[:len(s)] == s:
93 return 'xbm'
Guido van Rossum05b55e71992-06-03 16:48:44 +000094
95tests.append(test_xbm)
96
Guido van Rossum5cfa5df1993-06-23 09:30:50 +000097def test_jpeg(h, f):
Guido van Rossum54f22ed2000-02-04 15:10:34 +000098 """JPEG data in JFIF format"""
99 if h[6:10] == 'JFIF':
100 return 'jpeg'
Guido van Rossum5cfa5df1993-06-23 09:30:50 +0000101
102tests.append(test_jpeg)
Guido van Rossum1c34fc71992-05-27 14:06:59 +0000103
Guido van Rossum9e330741997-04-11 18:59:38 +0000104def test_bmp(h, f):
Guido van Rossum54f22ed2000-02-04 15:10:34 +0000105 if h[:2] == 'BM':
106 return 'bmp'
Tim Peters07e99cb2001-01-14 23:47:14 +0000107
Guido van Rossum9e330741997-04-11 18:59:38 +0000108tests.append(test_bmp)
109
110def test_png(h, f):
Guido van Rossum54f22ed2000-02-04 15:10:34 +0000111 if h[:8] == "\211PNG\r\n\032\n":
112 return 'png'
Guido van Rossum9e330741997-04-11 18:59:38 +0000113
114tests.append(test_png)
115
Guido van Rossum1c34fc71992-05-27 14:06:59 +0000116#--------------------#
117# Small test program #
118#--------------------#
119
120def test():
Guido van Rossum54f22ed2000-02-04 15:10:34 +0000121 import sys
122 recursive = 0
123 if sys.argv[1:] and sys.argv[1] == '-r':
124 del sys.argv[1:2]
125 recursive = 1
126 try:
127 if sys.argv[1:]:
128 testall(sys.argv[1:], recursive, 1)
129 else:
130 testall(['.'], recursive, 1)
131 except KeyboardInterrupt:
132 sys.stderr.write('\n[Interrupted]\n')
133 sys.exit(1)
Guido van Rossum1c34fc71992-05-27 14:06:59 +0000134
135def testall(list, recursive, toplevel):
Guido van Rossum54f22ed2000-02-04 15:10:34 +0000136 import sys
137 import os
138 for filename in list:
139 if os.path.isdir(filename):
140 print filename + '/:',
141 if recursive or toplevel:
142 print 'recursing down:'
143 import glob
144 names = glob.glob(os.path.join(filename, '*'))
145 testall(names, recursive, 0)
146 else:
147 print '*** directory (use -r) ***'
148 else:
149 print filename + ':',
150 sys.stdout.flush()
151 try:
152 print what(filename)
153 except IOError:
154 print '*** not found ***'