| 1 | n/a | """Recognize image file formats based on their first few bytes.""" |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | from os import PathLike |
|---|
| 4 | n/a | |
|---|
| 5 | n/a | __all__ = ["what"] |
|---|
| 6 | n/a | |
|---|
| 7 | n/a | #-------------------------# |
|---|
| 8 | n/a | # Recognize image headers # |
|---|
| 9 | n/a | #-------------------------# |
|---|
| 10 | n/a | |
|---|
| 11 | n/a | def what(file, h=None): |
|---|
| 12 | n/a | f = None |
|---|
| 13 | n/a | try: |
|---|
| 14 | n/a | if h is None: |
|---|
| 15 | n/a | if isinstance(file, (str, PathLike)): |
|---|
| 16 | n/a | f = open(file, 'rb') |
|---|
| 17 | n/a | h = f.read(32) |
|---|
| 18 | n/a | else: |
|---|
| 19 | n/a | location = file.tell() |
|---|
| 20 | n/a | h = file.read(32) |
|---|
| 21 | n/a | file.seek(location) |
|---|
| 22 | n/a | for tf in tests: |
|---|
| 23 | n/a | res = tf(h, f) |
|---|
| 24 | n/a | if res: |
|---|
| 25 | n/a | return res |
|---|
| 26 | n/a | finally: |
|---|
| 27 | n/a | if f: f.close() |
|---|
| 28 | n/a | return None |
|---|
| 29 | n/a | |
|---|
| 30 | n/a | |
|---|
| 31 | n/a | #---------------------------------# |
|---|
| 32 | n/a | # Subroutines per image file type # |
|---|
| 33 | n/a | #---------------------------------# |
|---|
| 34 | n/a | |
|---|
| 35 | n/a | tests = [] |
|---|
| 36 | n/a | |
|---|
| 37 | n/a | def test_jpeg(h, f): |
|---|
| 38 | n/a | """JPEG data in JFIF or Exif format""" |
|---|
| 39 | n/a | if h[6:10] in (b'JFIF', b'Exif'): |
|---|
| 40 | n/a | return 'jpeg' |
|---|
| 41 | n/a | |
|---|
| 42 | n/a | tests.append(test_jpeg) |
|---|
| 43 | n/a | |
|---|
| 44 | n/a | def test_png(h, f): |
|---|
| 45 | n/a | if h.startswith(b'\211PNG\r\n\032\n'): |
|---|
| 46 | n/a | return 'png' |
|---|
| 47 | n/a | |
|---|
| 48 | n/a | tests.append(test_png) |
|---|
| 49 | n/a | |
|---|
| 50 | n/a | def test_gif(h, f): |
|---|
| 51 | n/a | """GIF ('87 and '89 variants)""" |
|---|
| 52 | n/a | if h[:6] in (b'GIF87a', b'GIF89a'): |
|---|
| 53 | n/a | return 'gif' |
|---|
| 54 | n/a | |
|---|
| 55 | n/a | tests.append(test_gif) |
|---|
| 56 | n/a | |
|---|
| 57 | n/a | def test_tiff(h, f): |
|---|
| 58 | n/a | """TIFF (can be in Motorola or Intel byte order)""" |
|---|
| 59 | n/a | if h[:2] in (b'MM', b'II'): |
|---|
| 60 | n/a | return 'tiff' |
|---|
| 61 | n/a | |
|---|
| 62 | n/a | tests.append(test_tiff) |
|---|
| 63 | n/a | |
|---|
| 64 | n/a | def test_rgb(h, f): |
|---|
| 65 | n/a | """SGI image library""" |
|---|
| 66 | n/a | if h.startswith(b'\001\332'): |
|---|
| 67 | n/a | return 'rgb' |
|---|
| 68 | n/a | |
|---|
| 69 | n/a | tests.append(test_rgb) |
|---|
| 70 | n/a | |
|---|
| 71 | n/a | def test_pbm(h, f): |
|---|
| 72 | n/a | """PBM (portable bitmap)""" |
|---|
| 73 | n/a | if len(h) >= 3 and \ |
|---|
| 74 | n/a | h[0] == ord(b'P') and h[1] in b'14' and h[2] in b' \t\n\r': |
|---|
| 75 | n/a | return 'pbm' |
|---|
| 76 | n/a | |
|---|
| 77 | n/a | tests.append(test_pbm) |
|---|
| 78 | n/a | |
|---|
| 79 | n/a | def test_pgm(h, f): |
|---|
| 80 | n/a | """PGM (portable graymap)""" |
|---|
| 81 | n/a | if len(h) >= 3 and \ |
|---|
| 82 | n/a | h[0] == ord(b'P') and h[1] in b'25' and h[2] in b' \t\n\r': |
|---|
| 83 | n/a | return 'pgm' |
|---|
| 84 | n/a | |
|---|
| 85 | n/a | tests.append(test_pgm) |
|---|
| 86 | n/a | |
|---|
| 87 | n/a | def test_ppm(h, f): |
|---|
| 88 | n/a | """PPM (portable pixmap)""" |
|---|
| 89 | n/a | if len(h) >= 3 and \ |
|---|
| 90 | n/a | h[0] == ord(b'P') and h[1] in b'36' and h[2] in b' \t\n\r': |
|---|
| 91 | n/a | return 'ppm' |
|---|
| 92 | n/a | |
|---|
| 93 | n/a | tests.append(test_ppm) |
|---|
| 94 | n/a | |
|---|
| 95 | n/a | def test_rast(h, f): |
|---|
| 96 | n/a | """Sun raster file""" |
|---|
| 97 | n/a | if h.startswith(b'\x59\xA6\x6A\x95'): |
|---|
| 98 | n/a | return 'rast' |
|---|
| 99 | n/a | |
|---|
| 100 | n/a | tests.append(test_rast) |
|---|
| 101 | n/a | |
|---|
| 102 | n/a | def test_xbm(h, f): |
|---|
| 103 | n/a | """X bitmap (X10 or X11)""" |
|---|
| 104 | n/a | if h.startswith(b'#define '): |
|---|
| 105 | n/a | return 'xbm' |
|---|
| 106 | n/a | |
|---|
| 107 | n/a | tests.append(test_xbm) |
|---|
| 108 | n/a | |
|---|
| 109 | n/a | def test_bmp(h, f): |
|---|
| 110 | n/a | if h.startswith(b'BM'): |
|---|
| 111 | n/a | return 'bmp' |
|---|
| 112 | n/a | |
|---|
| 113 | n/a | tests.append(test_bmp) |
|---|
| 114 | n/a | |
|---|
| 115 | n/a | def test_webp(h, f): |
|---|
| 116 | n/a | if h.startswith(b'RIFF') and h[8:12] == b'WEBP': |
|---|
| 117 | n/a | return 'webp' |
|---|
| 118 | n/a | |
|---|
| 119 | n/a | tests.append(test_webp) |
|---|
| 120 | n/a | |
|---|
| 121 | n/a | def test_exr(h, f): |
|---|
| 122 | n/a | if h.startswith(b'\x76\x2f\x31\x01'): |
|---|
| 123 | n/a | return 'exr' |
|---|
| 124 | n/a | |
|---|
| 125 | n/a | tests.append(test_exr) |
|---|
| 126 | n/a | |
|---|
| 127 | n/a | #--------------------# |
|---|
| 128 | n/a | # Small test program # |
|---|
| 129 | n/a | #--------------------# |
|---|
| 130 | n/a | |
|---|
| 131 | n/a | def test(): |
|---|
| 132 | n/a | import sys |
|---|
| 133 | n/a | recursive = 0 |
|---|
| 134 | n/a | if sys.argv[1:] and sys.argv[1] == '-r': |
|---|
| 135 | n/a | del sys.argv[1:2] |
|---|
| 136 | n/a | recursive = 1 |
|---|
| 137 | n/a | try: |
|---|
| 138 | n/a | if sys.argv[1:]: |
|---|
| 139 | n/a | testall(sys.argv[1:], recursive, 1) |
|---|
| 140 | n/a | else: |
|---|
| 141 | n/a | testall(['.'], recursive, 1) |
|---|
| 142 | n/a | except KeyboardInterrupt: |
|---|
| 143 | n/a | sys.stderr.write('\n[Interrupted]\n') |
|---|
| 144 | n/a | sys.exit(1) |
|---|
| 145 | n/a | |
|---|
| 146 | n/a | def testall(list, recursive, toplevel): |
|---|
| 147 | n/a | import sys |
|---|
| 148 | n/a | import os |
|---|
| 149 | n/a | for filename in list: |
|---|
| 150 | n/a | if os.path.isdir(filename): |
|---|
| 151 | n/a | print(filename + '/:', end=' ') |
|---|
| 152 | n/a | if recursive or toplevel: |
|---|
| 153 | n/a | print('recursing down:') |
|---|
| 154 | n/a | import glob |
|---|
| 155 | n/a | names = glob.glob(os.path.join(filename, '*')) |
|---|
| 156 | n/a | testall(names, recursive, 0) |
|---|
| 157 | n/a | else: |
|---|
| 158 | n/a | print('*** directory (use -r) ***') |
|---|
| 159 | n/a | else: |
|---|
| 160 | n/a | print(filename + ':', end=' ') |
|---|
| 161 | n/a | sys.stdout.flush() |
|---|
| 162 | n/a | try: |
|---|
| 163 | n/a | print(what(filename)) |
|---|
| 164 | n/a | except OSError: |
|---|
| 165 | n/a | print('*** not found ***') |
|---|
| 166 | n/a | |
|---|
| 167 | n/a | if __name__ == '__main__': |
|---|
| 168 | n/a | test() |
|---|