"""Recognize image file formats based on their first few bytes."""
#-------------------------#
# Recognize image headers #
#-------------------------#
if isinstance(file, (str, PathLike)):
#---------------------------------#
# Subroutines per image file type #
#---------------------------------#
"""JPEG data in JFIF or Exif format"""
if h[6:10] in (b'JFIF', b'Exif'):
if h.startswith(b'\211PNG\r\n\032\n'):
"""GIF ('87 and '89 variants)"""
if h[:6] in (b'GIF87a', b'GIF89a'):
"""TIFF (can be in Motorola or Intel byte order)"""
if h[:2] in (b'MM', b'II'):
if h.startswith(b'\001\332'):
"""PBM (portable bitmap)"""
h[0] == ord(b'P') and h[1] in b'14' and h[2] in b' \t\n\r':
"""PGM (portable graymap)"""
h[0] == ord(b'P') and h[1] in b'25' and h[2] in b' \t\n\r':
"""PPM (portable pixmap)"""
h[0] == ord(b'P') and h[1] in b'36' and h[2] in b' \t\n\r':
if h.startswith(b'\x59\xA6\x6A\x95'):
"""X bitmap (X10 or X11)"""
if h.startswith(b'#define '):
if h.startswith(b'RIFF') and h[8:12] == b'WEBP':
if h.startswith(b'\x76\x2f\x31\x01'):
if sys.argv[1:] and sys.argv[1] == '-r':
testall(sys.argv[1:], recursive, 1)
testall(['.'], recursive, 1)
except KeyboardInterrupt:
sys.stderr.write('\n[Interrupted]\n')
def testall(list, recursive, toplevel):
if os.path.isdir(filename):
print(filename + '/:', end=' ')
if recursive or toplevel:
names = glob.glob(os.path.join(glob.escape(filename), '*'))
testall(names, recursive, 0)
print('*** directory (use -r) ***')
print(filename + ':', end=' ')
print('*** not found ***')
if __name__ == '__main__':