1 #-*-coding:utf-8-*-2 3 fromPILImportImage4 fromPylabImport*5 6 defcompare_pic_l (pic1,pic2):7 #Open the first picture8IM1 = Image.open (PIC1). CONVERT ('L')9 PrintIm1.format, im1.size, Im1.modeTen One #pixel value to array AAIM1 =np.transpose (Array (IM1)) - #im1.show () - theIm2=image.open (Pic2). CONVERT ('L') -Aim2=np.transpose (Array (im2)) - im2.show () - +dif= Np.zeros (300,307)) - + forXinchRange (Dif.shape[0]): A forYinchRange (dif.shape[1]): at ifAim1[x,y] = =Aim2[x,y]: -dif[x,y]=255 - Else: -dif[x,y]=0 - #Print Dif[x,y] - #Print dif in Print 'dif:', Dif.shape - tox=Im1.size[0] +Y=im1.size[1] - thec = Image.new ("L", (x, y), (255)) * #c.show () $ #paint with pixel grayscale valuesPanax Notoginseng forUinchRange (0,x): - forIinchRange (0,y): the #print type (Dif[i,u]) +ss=Int (dif[u,i]) A C.putpixel ((u,i), SS) the c.show () + - defcompare_colorful_pic (pic1,pic2): $ #Open the first one to save the pixel information to an array. $Im3_1 =Image.open (PIC1) -array_im1=np.transpose (Array (im3_1)) - PrintArray_im1.shape the - #Open the second one .Wuyiim33_ =Image.open (PIC2) theArray_im2=np.transpose (Array (im33_)) - PrintArray_im2.shape Wu - #make Poor AboutDIF_=ARRAY_IM2-array_im1 $ #Print dif_ - Printtype (dif_) - - #paint with three primary colors in pixel points APic_d = Image.new ("RGB", (Array_im1.shape[1],array_im1.shape[2]), (255)) + forXinchRange (array_im1.shape[1]): the forYinchRange (array_im1.shape[2]): - $Pic_d.putpixel ((x, y), (255-dif_[0,x,y],255-dif_[1,x,y],255-dif_[2, X, y])) the #Note that all two parameters are tuples;; First parameter: The number of pixels is wide * high, the second parameter is a three-color (three-channel) the the pic_d.show () thePic_d.save ('d.jpg') - in if __name__=="__main__": thecompare_pic_l ('2.jpg','2__.jpg') theCompare_colorful_pic ('2.jpg','2__.jpg')
Python pil compare picture pixels