#/usr/bin/python
#encoding:utf-8
from PIL import Image
import os,time
import sendresyslog
def calculate(image1, image2):
g = image1.histogram()
s = image2.histogram()
assert len(g) == len(s), "error"
data = []
for index in range(0, len(g)):
if g[index] != s[index]:
data.append(1 - abs(g[index] - s[index]) / max(g[index], s[index]))
else:
data.append(1)
return sum(data) / len(g)
def split_image(image, part_size):
pw, ph = part_size
w, h = image.size
sub_image_list = []
assert w % pw == h % ph == 0, "error"
for i in range(0, w, pw):
for j in range(0, h, ph):
sub_image = image.crop((i, j, i + pw, j + ph)).copy()
sub_image_list.append(sub_image)
return sub_image_list
def classfiy_histogram_with_split(image1, image2, size=(256, 256), part_size=(64, 64)):
'''
'image1' 和 'image2' 都是Image 对象.
可以通过'Image.open(path)'进行创建。
'size' 重新将 image 对象的尺寸进行重置,默认大小为256 * 256 .
'part_size' 定义了分割图片的大小.默认大小为64*64 .
返回值是 'image1' 和 'image2'对比后的相似度,相似度越高,图片越接近,达到100.0说明图片完全相同。
'''
img1 = image1.resize(size).convert("RGB")
sub_image1 = split_image(img1, part_size)
img2 = image2.resize(size).convert("RGB")
sub_image2 = split_image(img2, part_size)
sub_data = 0
for im1, im2 in zip(sub_image1, sub_image2):
sub_data += calculate(im1, im2)
x = size[0] / part_size[0]
y = size[1] / part_size[1]
pre = round((sub_data / (x * y)), 6)
return pre * 100
def identifyimgRun(newpath,oldpath):
for dirpath,dirnames,filenames in os.walk(newpath):
subdirname=dirpath.split('\\')[-1]
for filename in filenames:
FindTime=time.strftime('%Y%m%d%H%M%S',time.localtime(time.time()))
if len(filename)==0:
pass
else:
filepath = os.path.join(dirpath,filename)
oldfilepath=oldpath+'\\'+subdirname+'\\'+filename
newimge=Image.open(filepath)
oldimge=Image.open(oldfilepath)
flag=classfiy_histogram_with_split(newimge, oldimge)
if flag<100.0:
mes=' 告警时间:'+FindTime+'告警级别:2'+' 告警编号:8'+' 告警类型:网站图片变化'+' 机构名称:'+filename+' 告警内容:网站图片的发送变化'+' 异常图片路径:'+filepath
print(mes)
sendresyslog.sendresyslog(mes)
if __name__ == '__main__':
newpath = os.getcwd()+'\\img\\new'
oldpath = os.getcwd()+'\\img\\old'
identifyimgRun(newpath, oldpath)
#encoding:utf-8
from PIL import Image
import os,time
import sendresyslog
def calculate(image1, image2):
g = image1.histogram()
s = image2.histogram()
assert len(g) == len(s), "error"
data = []
for index in range(0, len(g)):
if g[index] != s[index]:
data.append(1 - abs(g[index] - s[index]) / max(g[index], s[index]))
else:
data.append(1)
return sum(data) / len(g)
def split_image(image, part_size):
pw, ph = part_size
w, h = image.size
sub_image_list = []
assert w % pw == h % ph == 0, "error"
for i in range(0, w, pw):
for j in range(0, h, ph):
sub_image = image.crop((i, j, i + pw, j + ph)).copy()
sub_image_list.append(sub_image)
return sub_image_list
def classfiy_histogram_with_split(image1, image2, size=(256, 256), part_size=(64, 64)):
'''
'image1' 和 'image2' 都是Image 对象.
可以通过'Image.open(path)'进行创建。
'size' 重新将 image 对象的尺寸进行重置,默认大小为256 * 256 .
'part_size' 定义了分割图片的大小.默认大小为64*64 .
返回值是 'image1' 和 'image2'对比后的相似度,相似度越高,图片越接近,达到100.0说明图片完全相同。
'''
img1 = image1.resize(size).convert("RGB")
sub_image1 = split_image(img1, part_size)
img2 = image2.resize(size).convert("RGB")
sub_image2 = split_image(img2, part_size)
sub_data = 0
for im1, im2 in zip(sub_image1, sub_image2):
sub_data += calculate(im1, im2)
x = size[0] / part_size[0]
y = size[1] / part_size[1]
pre = round((sub_data / (x * y)), 6)
return pre * 100
def identifyimgRun(newpath,oldpath):
for dirpath,dirnames,filenames in os.walk(newpath):
subdirname=dirpath.split('\\')[-1]
for filename in filenames:
FindTime=time.strftime('%Y%m%d%H%M%S',time.localtime(time.time()))
if len(filename)==0:
pass
else:
filepath = os.path.join(dirpath,filename)
oldfilepath=oldpath+'\\'+subdirname+'\\'+filename
newimge=Image.open(filepath)
oldimge=Image.open(oldfilepath)
flag=classfiy_histogram_with_split(newimge, oldimge)
if flag<100.0:
mes=' 告警时间:'+FindTime+'告警级别:2'+' 告警编号:8'+' 告警类型:网站图片变化'+' 机构名称:'+filename+' 告警内容:网站图片的发送变化'+' 异常图片路径:'+filepath
print(mes)
sendresyslog.sendresyslog(mes)
if __name__ == '__main__':
newpath = os.getcwd()+'\\img\\new'
oldpath = os.getcwd()+'\\img\\old'
identifyimgRun(newpath, oldpath)