def is_equal_with_tolerance(image1, image2, tolerance=Var.current("tolerance")): with allure.step("Comparing two images with tolerance: " + tolerance): image1_path = Img.root_path() + "/Data/Images/" + image1 image2_path = Img.root_path() + "/reports/images/" + image2 result = imgcompare.is_equal(image1_path, image2_path, tolerance=tolerance) allure.attach.file(image1_path, name=image1, attachment_type=allure.attachment_type.PNG) allure.attach.file(image2_path, name=image2, attachment_type=allure.attachment_type.PNG) if not result: if Var.env("snap") == "1": with allure.step("Copying the response file to the source file"): Img.copy_image(image2_path, image1_path) with allure.step("Attaching the diff image file"): image_diff_percent = imgcompare.image_diff_percent(image1_path, image2_path) allure.step("Difference Percentage for comparing images is" + image_diff_percent) diff_img_name = "diff_" + image1 + image2 + ".png" diff_img_path = Img.root_path() + "/reports/images/" + diff_img_name diffimg.diff(image1_path, image2_path, delete_diff_file=False, diff_img_file=diff_img_path, ignore_alpha=False) allure.attach.file(diff_img_path, name=diff_img_name, attachment_type=allure.attachment_type.PNG) assert (result is True), "Compared images are not equal even with tolerance"
def is_equal(image1, image2): with allure.step("Comparing two images"): try: image1_path = Img.root_path() + "/Data/Images/" + image1 + ".png" image2_path = Img.root_path() + "/reports/images/" + image2 + ".png" result = imgcompare.is_equal(image1_path, image2_path) allure.attach.file(image1_path, name=image1, attachment_type=allure.attachment_type.PNG) allure.attach.file(image2_path, name=image2, attachment_type=allure.attachment_type.PNG) except Exception as e: allure.step("Exception happened while comparing the image: " + str(e)) result = False if not result: if Var.env("snap") == "1": with allure.step("Copying the response file to the source file"): Img.copy_image(image1_path, image2_path) with allure.step("Attaching the diff image file"): image_diff_percent = imgcompare.image_diff_percent(image1_path, image2_path) allure.step("Difference Percentage for comparing images is" + str(image_diff_percent)) diff_img_name = "diff_" + image1 + image2 + ".png" diff_img_path = Img.root_path() + "/reports/images/" + diff_img_name diffimg.diff(image1_path, image2_path, delete_diff_file=False, diff_img_file=diff_img_path, ignore_alpha=False) allure.attach.file(diff_img_path, name=diff_img_name, attachment_type=allure.attachment_type.PNG) assert (result is True), "Compared images are not equal"
def main(): args = get_parser().parse_args() news = args.news dir_path = args.dir out_dir = pathlib.Path(args.output) if not out_dir.exists(): out_dir.mkdir() for i in sorted(glob.glob(f'{dir_path}/*.jpg')): nm = pynayzr.NewsModel(news, image_path=i) filename = os.path.splitext(os.path.basename(i))[0] nm.save_all(out_dir.joinpath(filename)) images = sorted(glob.glob(f'{out_dir}/*_title.jpg')) bottoms = sorted(glob.glob(f'{out_dir}/*_bottom.jpg')) # Compare base current = images[0] current_i = 0 # Unique title, (start, end), duration uniq = [] times = [] durations = [] # Static part's box # NOTE: This will compare with "bottom", not title if args.reference: cp_box = args.reference elif news in REFERENCE_BOX: cp_box = REFERENCE_BOX[news] else: # XXX: Should warn user? cp_box = (0, 0, 1, 1) cp = Image.open(bottoms[0]).crop(cp_box) # Couting for continuous frame continuous = 0 # Compare the images prev = current for index, i in enumerate(images[1:]): continuous += 1 if not imgcompare.is_equal(current, i, 12): if continuous > args.threshold: im = Image.open(bottoms[current_i + 1]) if imgcompare.is_equal(cp, im.crop(cp_box), 3): uniq.append(prev) times.append((get_frame_time(current), get_frame_time(i))) durations.append(times[-1][1] - times[-1][0]) current = i current_i = index continuous = 0 prev = i # Print the information print( len(images), len(uniq), f'mean: {statistics.mean(durations):.2f}, ' f'stdev: {statistics.stdev(durations):.2f}, ' f'median: {statistics.median(durations)}') # Output TIME_DURATION_SPACE = 100 w, h = Image.open(uniq[0]).size w += TIME_DURATION_SPACE # Give some space for time im = Image.new('RGB', (w, h * len(uniq)), '#FFFFFF') for index, i in enumerate(uniq): duration = ' ~ '.join( [get_time(times[index][0]), get_time(times[index][1] - 1)]) im.paste(Image.open(i), (0, h * index)) ImageDraw.Draw(im).text( (w - (TIME_DURATION_SPACE - 10), h * index + 10), duration, (0, 0, 0)) im.save('out.jpg')
def test_is_equal(self): self.assertTrue(imgcompare.is_equal(JPG_CAT, JPG_CAT_SLIGHT_DIFF, 0.5)) self.assertFalse(imgcompare.is_equal(JPG_CAT, JPG_CAT_SLIGHT_DIFF, 0.2)) self.assertTrue(imgcompare.is_equal(JPG_BLACK, JPG_BLACK)) self.assertTrue(imgcompare.is_equal(JPG_BLACK, JPG_BLACK, 0.2))
def test_upgrade_10(): img = upgradeTex(layeredImage, 0) imgClean = cleanImg(img.getFlattenLayers()) imgClean.save(f"{THISDIR}/data/out_10.png") assert imgcompare.is_equal(imgClean, f"{THISDIR}/data/out_10_expected.png")