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