blob: 6ee45daab8bc8fe708817eeeb0c5d5e11c324121 [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:
Georg Brandl3dbca812008-07-23 16:10:53 +000011 if isinstance(file, str):
Guido van Rossum54f22ed2000-02-04 15:10:34 +000012 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
Benjamin Peterson0b952902008-08-16 16:48:16 +000037def test_jpeg(h, f):
38 """JPEG data in JFIF or Exif format"""
39 if h[6:10] in (b'JFIF', b'Exif'):
40 return 'jpeg'
Guido van Rossum1c34fc71992-05-27 14:06:59 +000041
Benjamin Peterson0b952902008-08-16 16:48:16 +000042tests.append(test_jpeg)
43
44def test_png(h, f):
45 if h.startswith(b'\211PNG\r\n\032\n'):
46 return 'png'
47
48tests.append(test_png)
Guido van Rossum1c34fc71992-05-27 14:06:59 +000049
50def test_gif(h, f):
Guido van Rossum54f22ed2000-02-04 15:10:34 +000051 """GIF ('87 and '89 variants)"""
Barry Warsaw72937f32007-08-12 14:37:20 +000052 if h[:6] in (b'GIF87a', b'GIF89a'):
Guido van Rossum54f22ed2000-02-04 15:10:34 +000053 return 'gif'
Guido van Rossum1c34fc71992-05-27 14:06:59 +000054
55tests.append(test_gif)
56
Benjamin Peterson0b952902008-08-16 16:48:16 +000057def test_tiff(h, f):
58 """TIFF (can be in Motorola or Intel byte order)"""
59 if h[:2] in (b'MM', b'II'):
60 return 'tiff'
61
62tests.append(test_tiff)
63
64def test_rgb(h, f):
65 """SGI image library"""
66 if h.startswith(b'\001\332'):
67 return 'rgb'
68
69tests.append(test_rgb)
70
Guido van Rossum2db91351992-10-18 17:09:59 +000071def test_pbm(h, f):
Guido van Rossum54f22ed2000-02-04 15:10:34 +000072 """PBM (portable bitmap)"""
73 if len(h) >= 3 and \
Guido van Rossumc9341282007-08-13 17:50:00 +000074 h[0] == ord(b'P') and h[1] in b'14' and h[2] in b' \t\n\r':
Guido van Rossum54f22ed2000-02-04 15:10:34 +000075 return 'pbm'
Guido van Rossum1c34fc71992-05-27 14:06:59 +000076
Guido van Rossum2db91351992-10-18 17:09:59 +000077tests.append(test_pbm)
78
79def test_pgm(h, f):
Guido van Rossum54f22ed2000-02-04 15:10:34 +000080 """PGM (portable graymap)"""
81 if len(h) >= 3 and \
Guido van Rossumc9341282007-08-13 17:50:00 +000082 h[0] == ord(b'P') and h[1] in b'25' and h[2] in b' \t\n\r':
Guido van Rossum54f22ed2000-02-04 15:10:34 +000083 return 'pgm'
Guido van Rossum2db91351992-10-18 17:09:59 +000084
85tests.append(test_pgm)
86
87def test_ppm(h, f):
Guido van Rossum54f22ed2000-02-04 15:10:34 +000088 """PPM (portable pixmap)"""
89 if len(h) >= 3 and \
Guido van Rossumc9341282007-08-13 17:50:00 +000090 h[0] == ord(b'P') and h[1] in b'36' and h[2] in b' \t\n\r':
Guido van Rossum54f22ed2000-02-04 15:10:34 +000091 return 'ppm'
Guido van Rossum2db91351992-10-18 17:09:59 +000092
93tests.append(test_ppm)
Guido van Rossum1c34fc71992-05-27 14:06:59 +000094
Guido van Rossum1c34fc71992-05-27 14:06:59 +000095def test_rast(h, f):
Guido van Rossum54f22ed2000-02-04 15:10:34 +000096 """Sun raster file"""
Guido van Rossumc9341282007-08-13 17:50:00 +000097 if h.startswith(b'\x59\xA6\x6A\x95'):
Guido van Rossum54f22ed2000-02-04 15:10:34 +000098 return 'rast'
Guido van Rossum1c34fc71992-05-27 14:06:59 +000099
100tests.append(test_rast)
101
Guido van Rossum05b55e71992-06-03 16:48:44 +0000102def test_xbm(h, f):
Guido van Rossum54f22ed2000-02-04 15:10:34 +0000103 """X bitmap (X10 or X11)"""
Guido van Rossumc9341282007-08-13 17:50:00 +0000104 if h.startswith(b'#define '):
Guido van Rossum54f22ed2000-02-04 15:10:34 +0000105 return 'xbm'
Guido van Rossum05b55e71992-06-03 16:48:44 +0000106
107tests.append(test_xbm)
108
Guido van Rossum9e330741997-04-11 18:59:38 +0000109def test_bmp(h, f):
Guido van Rossumc9341282007-08-13 17:50:00 +0000110 if h.startswith(b'BM'):
Guido van Rossum54f22ed2000-02-04 15:10:34 +0000111 return 'bmp'
Tim Peters07e99cb2001-01-14 23:47:14 +0000112
Guido van Rossum9e330741997-04-11 18:59:38 +0000113tests.append(test_bmp)
114
Guido van Rossum1c34fc71992-05-27 14:06:59 +0000115#--------------------#
116# Small test program #
117#--------------------#
118
119def test():
Guido van Rossum54f22ed2000-02-04 15:10:34 +0000120 import sys
121 recursive = 0
122 if sys.argv[1:] and sys.argv[1] == '-r':
123 del sys.argv[1:2]
124 recursive = 1
125 try:
126 if sys.argv[1:]:
127 testall(sys.argv[1:], recursive, 1)
128 else:
129 testall(['.'], recursive, 1)
130 except KeyboardInterrupt:
131 sys.stderr.write('\n[Interrupted]\n')
132 sys.exit(1)
Guido van Rossum1c34fc71992-05-27 14:06:59 +0000133
134def testall(list, recursive, toplevel):
Guido van Rossum54f22ed2000-02-04 15:10:34 +0000135 import sys
136 import os
137 for filename in list:
138 if os.path.isdir(filename):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000139 print(filename + '/:', end=' ')
Guido van Rossum54f22ed2000-02-04 15:10:34 +0000140 if recursive or toplevel:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000141 print('recursing down:')
Guido van Rossum54f22ed2000-02-04 15:10:34 +0000142 import glob
143 names = glob.glob(os.path.join(filename, '*'))
144 testall(names, recursive, 0)
145 else:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000146 print('*** directory (use -r) ***')
Guido van Rossum54f22ed2000-02-04 15:10:34 +0000147 else:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000148 print(filename + ':', end=' ')
Guido van Rossum54f22ed2000-02-04 15:10:34 +0000149 sys.stdout.flush()
150 try:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000151 print(what(filename))
Guido van Rossum54f22ed2000-02-04 15:10:34 +0000152 except IOError:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000153 print('*** not found ***')
Barry Warsaw72937f32007-08-12 14:37:20 +0000154
155if __name__ == '__main__':
156 test()