codekingpro/portable-devtools
114k
1# A vendored copy of Python's imghdr module, which is slated for removal in Python 3.13.2#3# Source: https://github.com/python/cpython/blob/3.12/Lib/imghdr.py4# SPDX-License-Identifier: PSF-2.05 6"""Recognize image file formats based on their first few bytes."""7 8from os import PathLike9import warnings10 11__all__ = ["what"]12 13 14# warnings._deprecated(__name__, remove=(3, 13))15 16 17#-------------------------#18# Recognize image headers #19#-------------------------#20 21def what(file, h=None):22 """Return the type of image contained in a file or byte stream."""23 f = None24 try:25 if h is None:26 if isinstance(file, (str, PathLike)):27 f = open(file, 'rb')28 h = f.read(32)29 else:30 location = file.tell()31 h = file.read(32)32 file.seek(location)33 for tf in tests:34 res = tf(h, f)35 if res:36 return res37 finally:38 if f: f.close()39 return None40 41 42#---------------------------------#43# Subroutines per image file type #44#---------------------------------#45 46tests = []47 48def test_jpeg(h, f):49 """Test for JPEG data with JFIF or Exif markers; and raw JPEG."""50 if h[6:10] in (b'JFIF', b'Exif'):51 return 'jpeg'52 elif h[:4] == b'\xff\xd8\xff\xdb':53 return 'jpeg'54 55tests.append(test_jpeg)56 57def test_png(h, f):58 """Verify if the image is a PNG."""59 if h.startswith(b'\211PNG\r\n\032\n'):60 return 'png'61 62tests.append(test_png)63 64def test_gif(h, f):65 """Verify if the image is a GIF ('87 or '89 variants)."""66 if h[:6] in (b'GIF87a', b'GIF89a'):67 return 'gif'68 69tests.append(test_gif)70 71def test_tiff(h, f):72 """Verify if the image is a TIFF (can be in Motorola or Intel byte order)."""73 if h[:2] in (b'MM', b'II'):74 return 'tiff'75 76tests.append(test_tiff)77 78def test_rgb(h, f):79 """test for the SGI image library."""80 if h.startswith(b'\001\332'):81 return 'rgb'82 83tests.append(test_rgb)84 85def test_pbm(h, f):86 """Verify if the image is a PBM (portable bitmap)."""87 if len(h) >= 3 and \88 h[0] == ord(b'P') and h[1] in b'14' and h[2] in b' \t\n\r':89 return 'pbm'90 91tests.append(test_pbm)92 93def test_pgm(h, f):94 """Verify if the image is a PGM (portable graymap)."""95 if len(h) >= 3 and \96 h[0] == ord(b'P') and h[1] in b'25' and h[2] in b' \t\n\r':97 return 'pgm'98 99tests.append(test_pgm)100 101def test_ppm(h, f):102 """Verify if the image is a PPM (portable pixmap)."""103 if len(h) >= 3 and \104 h[0] == ord(b'P') and h[1] in b'36' and h[2] in b' \t\n\r':105 return 'ppm'106 107tests.append(test_ppm)108 109def test_rast(h, f):110 """test for the Sun raster file."""111 if h.startswith(b'\x59\xA6\x6A\x95'):112 return 'rast'113 114tests.append(test_rast)115 116def test_xbm(h, f):117 """Verify if the image is a X bitmap (X10 or X11)."""118 if h.startswith(b'#define '):119 return 'xbm'120 121tests.append(test_xbm)122 123def test_bmp(h, f):124 """Verify if the image is a BMP file."""125 if h.startswith(b'BM'):126 return 'bmp'127 128tests.append(test_bmp)129 130def test_webp(h, f):131 """Verify if the image is a WebP."""132 if h.startswith(b'RIFF') and h[8:12] == b'WEBP':133 return 'webp'134 135tests.append(test_webp)136 137def test_exr(h, f):138 """verify is the image ia a OpenEXR fileOpenEXR."""139 if h.startswith(b'\x76\x2f\x31\x01'):140 return 'exr'141 142tests.append(test_exr)143 