blob: 2fbc9661eb91aea93760826e325aeba2ee8e36e7 [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
Raymond Hettinger97db05d2005-01-07 08:15:41 +0000104def test_exif(h, f):
105 """JPEG data in Exif format"""
106 if h[6:10] == 'Exif':
107 return 'jpeg'
108
109tests.append(test_exif)
110
Guido van Rossum9e330741997-04-11 18:59:38 +0000111def test_bmp(h, f):
Guido van Rossum54f22ed2000-02-04 15:10:34 +0000112 if h[:2] == 'BM':
113 return 'bmp'
Tim Peters07e99cb2001-01-14 23:47:14 +0000114
Guido van Rossum9e330741997-04-11 18:59:38 +0000115tests.append(test_bmp)
116
117def test_png(h, f):
Guido van Rossum54f22ed2000-02-04 15:10:34 +0000118 if h[:8] == "\211PNG\r\n\032\n":
119 return 'png'
Guido van Rossum9e330741997-04-11 18:59:38 +0000120
121tests.append(test_png)
122
Guido van Rossum1c34fc71992-05-27 14:06:59 +0000123#--------------------#
124# Small test program #
125#--------------------#
126
127def test():
Guido van Rossum54f22ed2000-02-04 15:10:34 +0000128 import sys
129 recursive = 0
130 if sys.argv[1:] and sys.argv[1] == '-r':
131 del sys.argv[1:2]
132 recursive = 1
133 try:
134 if sys.argv[1:]:
135 testall(sys.argv[1:], recursive, 1)
136 else:
137 testall(['.'], recursive, 1)
138 except KeyboardInterrupt:
139 sys.stderr.write('\n[Interrupted]\n')
140 sys.exit(1)
Guido van Rossum1c34fc71992-05-27 14:06:59 +0000141
142def testall(list, recursive, toplevel):
Guido van Rossum54f22ed2000-02-04 15:10:34 +0000143 import sys
144 import os
145 for filename in list:
146 if os.path.isdir(filename):
147 print filename + '/:',
148 if recursive or toplevel:
149 print 'recursing down:'
150 import glob
151 names = glob.glob(os.path.join(filename, '*'))
152 testall(names, recursive, 0)
153 else:
154 print '*** directory (use -r) ***'
155 else:
156 print filename + ':',
157 sys.stdout.flush()
158 try:
159 print what(filename)
160 except IOError:
161 print '*** not found ***'