0001 """Recognize image file formats based on their first few bytes.""" 0002 0003 __all__ = ["what"] 0004 0005 #-------------------------# 0006 # Recognize image headers # 0007 #-------------------------# 0008 0009 def what(file, h=None): 0010 if h is None: 0011 if type(file) == type(''): 0012 f = open(file, 'rb') 0013 h = f.read(32) 0014 else: 0015 location = file.tell() 0016 h = file.read(32) 0017 file.seek(location) 0018 f = None 0019 else: 0020 f = None 0021 try: 0022 for tf in tests: 0023 res = tf(h, f) 0024 if res: 0025 return res 0026 finally: 0027 if f: f.close() 0028 return None 0029 0030 0031 #---------------------------------# 0032 # Subroutines per image file type # 0033 #---------------------------------# 0034 0035 tests = [] 0036 0037 def test_rgb(h, f): 0038 """SGI image library""" 0039 if h[:2] == '\001\332': 0040 return 'rgb' 0041 0042 tests.append(test_rgb) 0043 0044 def test_gif(h, f): 0045 """GIF ('87 and '89 variants)""" 0046 if h[:6] in ('GIF87a', 'GIF89a'): 0047 return 'gif' 0048 0049 tests.append(test_gif) 0050 0051 def test_pbm(h, f): 0052 """PBM (portable bitmap)""" 0053 if len(h) >= 3 and \ 0054 h[0] == 'P' and h[1] in '14' and h[2] in ' \t\n\r': 0055 return 'pbm' 0056 0057 tests.append(test_pbm) 0058 0059 def test_pgm(h, f): 0060 """PGM (portable graymap)""" 0061 if len(h) >= 3 and \ 0062 h[0] == 'P' and h[1] in '25' and h[2] in ' \t\n\r': 0063 return 'pgm' 0064 0065 tests.append(test_pgm) 0066 0067 def test_ppm(h, f): 0068 """PPM (portable pixmap)""" 0069 if len(h) >= 3 and \ 0070 h[0] == 'P' and h[1] in '36' and h[2] in ' \t\n\r': 0071 return 'ppm' 0072 0073 tests.append(test_ppm) 0074 0075 def test_tiff(h, f): 0076 """TIFF (can be in Motorola or Intel byte order)""" 0077 if h[:2] in ('MM', 'II'): 0078 return 'tiff' 0079 0080 tests.append(test_tiff) 0081 0082 def test_rast(h, f): 0083 """Sun raster file""" 0084 if h[:4] == '\x59\xA6\x6A\x95': 0085 return 'rast' 0086 0087 tests.append(test_rast) 0088 0089 def test_xbm(h, f): 0090 """X bitmap (X10 or X11)""" 0091 s = '#define ' 0092 if h[:len(s)] == s: 0093 return 'xbm' 0094 0095 tests.append(test_xbm) 0096 0097 def test_jpeg(h, f): 0098 """JPEG data in JFIF format""" 0099 if h[6:10] == 'JFIF': 0100 return 'jpeg' 0101 0102 tests.append(test_jpeg) 0103 0104 def test_bmp(h, f): 0105 if h[:2] == 'BM': 0106 return 'bmp' 0107 0108 tests.append(test_bmp) 0109 0110 def test_png(h, f): 0111 if h[:8] == "\211PNG\r\n\032\n": 0112 return 'png' 0113 0114 tests.append(test_png) 0115 0116 #--------------------# 0117 # Small test program # 0118 #--------------------# 0119 0120 def test(): 0121 import sys 0122 recursive = 0 0123 if sys.argv[1:] and sys.argv[1] == '-r': 0124 del sys.argv[1:2] 0125 recursive = 1 0126 try: 0127 if sys.argv[1:]: 0128 testall(sys.argv[1:], recursive, 1) 0129 else: 0130 testall(['.'], recursive, 1) 0131 except KeyboardInterrupt: 0132 sys.stderr.write('\n[Interrupted]\n') 0133 sys.exit(1) 0134 0135 def testall(list, recursive, toplevel): 0136 import sys 0137 import os 0138 for filename in list: 0139 if os.path.isdir(filename): 0140 print filename + '/:', 0141 if recursive or toplevel: 0142 print 'recursing down:' 0143 import glob 0144 names = glob.glob(os.path.join(filename, '*')) 0145 testall(names, recursive, 0) 0146 else: 0147 print '*** directory (use -r) ***' 0148 else: 0149 print filename + ':', 0150 sys.stdout.flush() 0151 try: 0152 print what(filename) 0153 except IOError: 0154 print '*** not found ***' 0155
Generated by PyXR 0.9.4