def rmsdiff_2011(image1, image2): """Calculate the root-mean-square difference between two images http://code.activestate.com/recipes/577630/ """ im1 = lib.open_image(image1) im2 = lib.open_image(image2) if im1.size != im2.size: # print("rmsdiff_2011", im1.size, im2.size) # print("Warning: Size of the two images not equal!") # lib.write_log("rmsdiff_2011", im1.size, im2.size) # lib.write_log("Warning: Size of the two images not equal!") return "Warning: Size of the two images not equal!" + str(im1.size) + str(im2.size) diff = ImageChops.difference(im1.convert("RGBA"), im2.convert("RGBA")) h = diff.histogram() sq = (value * (idx ** 2) for idx, value in enumerate(h)) sum_of_squares = sum(sq) rms = math.sqrt(sum_of_squares / float(im1.size[0] * im1.size[1])) return rms
def generate_compare_image(image1, image2): folder = os.path.dirname(image1) img1_name = os.path.basename(image1) img2_name = os.path.basename(image2) img1_name = ".".join(img1_name.split(".")[:-1]) img2_name = ".".join(img2_name.split(".")[:-1]) image1 = lib.open_image(image1) image2 = lib.open_image(image2) diff_image = ImageChops.difference(image1.convert("RGBA"), image2.convert("RGBA")) diff_image = diff_image.convert("L") diff_image = diff_image.point(([0] + ([255] * 255))) new_image = diff_image.convert("RGB") if image1.size == image2.size: new_image.paste(image2, mask=diff_image) else: print ("two images size not equal:", image1.size, image2.size) # lib.write_log("two images size not equal:", image1.size, image2.size) new_image.save(os.path.join(folder, img1_name + "__VS__" + img2_name + ".png"))