1 # -*- coding: utf-8 -*- 2 3 from PIL import Image 4 from pylab import * 5 6 def compare_pic_L(pic1,pic2): 7 #打开第一张图片 8 im1 = Image.open(pic1).convert('L') 9 print im1.format ,im1.size, im1.mode10 11 #像素值转数组12 aim1 = np.transpose(array(im1))13 #im1.show()14 15 im2=Image.open(pic2).convert('L')16 aim2=np.transpose(array(im2))17 im2.show()18 19 dif= np.zeros((300,307))20 21 for x in range(dif.shape[0]):22 for y in range(dif.shape[1]):23 if aim1[x,y] == aim2[x,y] :24 dif[x,y]=25525 else:26 dif[x,y]=027 #print dif[x,y]28 #print dif29 print 'dif:',dif.shape30 31 x=im1.size[0]32 y=im1.size[1]33 34 c = Image.new("L",(x,y),(255))35 #c.show()36 #用 像素灰度值画图37 for u in range(0,x):38 for i in range(0,y):39 #print type(dif[i,u])40 ss=int(dif[u,i])41 c.putpixel((u,i),ss)42 c.show()43 44 def compare_colorful_pic(pic1,pic2):45 #打开第一张 把像素点信息保存到数组46 im3_1 = Image.open(pic1)47 array_im1=np.transpose(array(im3_1))48 print array_im1.shape49 50 #打开第二张51 im33_ = Image.open(pic2)52 array_im2=np.transpose(array(im33_))53 print array_im2.shape54 55 #作差56 dif_=array_im2 - array_im157 #print dif_58 print type(dif_)59 60 #用像素点三基色画图61 pic_D = Image.new("RGB",(array_im1.shape[1],array_im1.shape[2]),(255))62 for x in range(array_im1.shape[1]):63 for y in range(array_im1.shape[2]):64 65 pic_D.putpixel((x,y),(255-dif_[0,x,y],255-dif_[1,x,y],255-dif_[2,x,y]))66 #注意 两个参数都为元组;;第一个参数:像素数量 宽*高,第二个参数为 三基色 (三通道)67 68 pic_D.show()69 pic_D.save('d.jpg')70 71 if __name__ == "__main__":72 compare_pic_L('2.jpg','2__.jpg')73 compare_colorful_pic('2.jpg','2__.jpg')