if len(i1.getbands()) == 0: code example

Example 1: if len(i1.getbands()) == 1

from PIL import Image, ImageChops, ImageDraw, ImageFont
def check_image_files(self, file1, file2, file3)                :
       ''' Check two image files

       ``file1``: absolute path to the first file

       ``file2``: absolute path to the second file

       ``file3``: absolute path to the compare file
       '''

       self.builtin.log("File1: %s" %file1)
       self.builtin.log("File2: %s" %file2)

       point_table = ([0] + ([255] * 255))

       f1 = Image.open(file1)
       f2 = Image.open(file2)

       diff = ImageChops.difference(f1, f2)
       diff = diff.convert('L')
       diff = diff.point(point_table)
       f3 = diff.convert('RGB')
       f3.paste(f2, mask=diff)        
       f3.save(file3)

Example 2: if len(i1.getbands()) == 1

from PIL import Image

i1 = Image.open('./image_1.png')

for i in range(18):
	i2 = Image.open('./image_%s.png'% i)
	pairs = zip(i1.getdata(), i2.getdata())
	if len(i1.getbands()) == 1:
		# for gray-scale jpegs
		dif = sum(abs(p1-p2) for p1,p2 in pairs)
	else:
		dif = sum(abs(c1-c2) for p1,p2 in pairs for c1,c2 in zip(p1,p2))
	ncomponents = i1.size[0] * i1.size[1] * 3
	print ("Difference (percentage):", (dif / 255.0 * 100) / ncomponents)