imghdr.py 480 B

1234567891011121314
  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'),
  5. }
  6. def what(path):
  7. """Detect format of image (Currently supports jpeg, png, webp only)
  8. Ref: https://github.com/python/cpython/blob/3.10/Lib/imghdr.py
  9. """
  10. with open(path, 'rb') as f:
  11. head = f.read(12)
  12. return next((type_ for type_, test in tests.items() if test(head)), None)