]>
Commit | Line | Data |
---|---|---|
5792c950 | 1 | tests = { |
2 | 'webp': lambda h: h[0:4] == b'RIFF' and h[8:] == b'WEBP', | |
3 | 'png': lambda h: h[:8] == b'\211PNG\r\n\032\n', | |
4 | 'jpeg': lambda h: h[6:10] in (b'JFIF', b'Exif'), | |
b4daacb4 | 5 | 'gif': lambda h: h[:6] in (b'GIF87a', b'GIF89a'), |
5792c950 | 6 | } |
7 | ||
8 | ||
b4daacb4 | 9 | def what(file=None, h=None): |
10 | """Detect format of image (Currently supports jpeg, png, webp, gif only) | |
5792c950 | 11 | Ref: https://github.com/python/cpython/blob/3.10/Lib/imghdr.py |
12 | """ | |
b4daacb4 | 13 | if h is None: |
14 | with open(file, 'rb') as f: | |
15 | h = f.read(12) | |
16 | return next((type_ for type_, test in tests.items() if test(h)), None) |