最近在研究如果将测试结果保存成图片,如何能知道测试是不是通过,其中明显要设计到图像的对比算法。经过了一些研究,最终选择了OpenCV做为实现的基本技术。
OpenCV真的是个强大的东西,相对于Python其他的一些图像库,这个东西更加复杂,也非常灵活,我就直接上代码了
代码有三个部分,一个部分是核心的图像比对,一个是前台的html显示,还有一个就是基于uwsgi的胶水代码
基于Ubuntu 12.04 LTS, python版本2.7
1. 核心图像比对 diff_img.py
import cv2
import sys
import numpy as np
def main(img1p,img2p):
img1 = cv2.imread(img1p)
img2 = cv2.imread(img2p)
img3 = img2
height = img1.shape[0]
width = img1.shape[1]
if img1.shape[0] == img2.shape[0] and img1.shape[1] == img2.shape[1]:
diff = False
for i in range (0,width):
for j in range (0,height):
try:
if color_diff(img1[j,i],img2[j,i]) != True:
img3[j,i] = [0,0,255]
diff = True
except:
pass
if diff:
cv2.imwrite("uploads/wrong.png", img3)
print "yes"
else:
print "no"
else:
print "sizewrong"
def color_diff(img1p, img2p):
if len(img1p) != len(img2p):
return