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