#coding:utf-8
import cv2
import matplotlib.pyplot as plt
'''
新增多个list,将所有不同颜色部分按照一定范围区域划分为多个list,再分别对这多个list画框
1.只对品质、大小一样的图片做对比,(UI自动化截图符合此规格)
2.需要注意的是,当不同点过多,品质过高的图片,可能会出现内存溢出的错误(主要是因为所有像素点均存储于单个list中, 32位python的限制是 536870912 个元素)
3.存在多处不同时,目前以100个像素为基准划分
——jun
'''
img_t1 = cv2.imread("./t2.png")
img_t2 = cv2.imread("./t1.png")
print img_t1.shape
print img_t2.shape
if img_t1.shape != img_t2.shape:
print "cannot do it"
else:
img_list = list(img_t1.shape)
img_max = max(img_list[0],img_list[1])
img_min = min(img_list[0],img_list[1])
diff,multi_diss_list,name_list = [],[],[]
temp_x,temp_y = 0,0
name_i = 0
def make_diff(i,j):
temp_diff = []
temp_diff.append(i)
temp_diff.append(j)
diff.append(temp_diff)
def make_list(i,j,flag=0):
global multi_diss_list
if flag==0:
temp = []
temp.append(i)
temp.append(j)
multi_diss_list[-1].append(temp)
else:
names = locals()
names["diff_list%s"%flag] = []
new_list = names["diff_list%s"%flag]
temp = []
temp.append(i)
temp.append(j)
new_list.append(temp)
multi_diss_list.append(new_list)
def multi_list(mlist):
global temp_y,temp_x,name_i
for i in mlist:
if i[0]-temp_x>100:
name_i+=1
make_list(i[0],i[1], flag=name_i)
temp_x = i[0]
elif i[1]-temp_y>100:
name_i += 1
make_list(i[0],i[1], flag=name_i)
temp_y = i[1]
else:
make_list(i[0],i[1])
def make_rectangle(mlist):
temp_x_min,temp_x_max,temp_y_min,temp_y_max = mlist[0][0],mlist[0][0],mlist[0][1],mlist[0][1]
for i in mlist:
if i[0]>temp_x_max:
temp_x_max = i[0]
elif i[0]<=temp_x_min:
temp_x_min = i[0]
for i in mlist:
if i[1]>temp_y_max:
temp_y_max = i[1]
elif i[1]<=temp_y_min:
temp_y_min = i[1]
return (temp_x_min,temp_y_min,temp_x_max,temp_y_max)
for i in xrange(0,img_max):
for j in xrange(0,img_min):
if img_t1[j][i].all() == img_t2[j][i].all():
pass
else:
make_diff(i,j)
if len(diff) == 0:
print "all the same"
else:
print len(diff)
multi_list(mlist=diff)
for i in multi_diss_list:
if len(i) == 1:
pass
else:
left_x,left_y,right_x,right_y = make_rectangle(i)
left_top = (left_x,left_y)
right_bottom = (right_x,right_y)
cv2.rectangle(img_t2, left_top, right_bottom, (0, 0, 255), thickness=2)
cv2.imwrite('diffPIC.jpg', img_t2)