Skip to content

Commit

Permalink
Merge pull request #145 from googlefonts/img-diff
Browse files Browse the repository at this point in the history
Output images with difference blend mode
  • Loading branch information
m4rc1e committed May 15, 2024
2 parents 15c910c + c594bd7 commit f4c94e4
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/diffenator2/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ def main(**kwargs):
)
diff_parser.add_argument("--no-diffenator", default=False, action="store_true")
diff_parser.add_argument("--threshold", "-t", type=float, default=THRESHOLD)
diff_parser.add_argument("--precision", default=FONT_SIZE)
diff_parser.add_argument("--precision", default=FONT_SIZE, type=int)
# TODO this can just be precision
diff_parser.add_argument("--font-size", default=FONT_SIZE)
diff_parser.add_argument("--font-size", default=FONT_SIZE, type=int)
diff_parser.add_argument(
"--no-tables", action="store_true", help="Skip diffing font tables"
)
Expand Down
1 change: 1 addition & 0 deletions src/diffenator2/screenshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def take_png(self, browser: any, dst_dir: str, javascript: str = ""):
body_el = browser.find_element(By.TAG_NAME, "html")
browser.set_window_size(self.width, body_el.size["height"])
browser.save_screenshot(filename)
return filename

def take_gif(self, url: str, dst_dir: str, font_toggle):
before_fp = os.path.join(dst_dir, "before")
Expand Down
11 changes: 9 additions & 2 deletions src/diffenator2/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,6 @@ def gen_gifs(dir1: str, dir2: str, dst_dir: str):
dir2_imgs = set(f for f in os.listdir(dir2) if f.endswith(("jpg", "png")))
shared_imgs = dir1_imgs & dir2_imgs
for img in shared_imgs:
# We use apng format since a gif's dimensions are limited to 64kx64k
# we have encountered many diffs which are taller than this.
gif_filename = img[:-4] + ".gif"
img_a_path = os.path.join(dir1, img)
img_b_path = os.path.join(dir2, img)
Expand All @@ -146,6 +144,15 @@ def gen_gif(img_a, img_b, dst):
img_a = img_a.crop(crop_box)
img_b = img_b.crop(crop_box)
img_a.save(dst, save_all=True, append_images=[img_b], loop=10000, duration=1000)
gen_img_difference(img_a, img_b, dst.replace(".gif", "_diff.png"))


def gen_img_difference(img_a, img_b, dst: str):
from PIL import ImageChops
img_a = img_a.convert("RGB")
img_b = img_b.convert("RGB")
img = ImageChops.difference(img_a, img_b)
img.save(dst)


@lru_cache()
Expand Down
2 changes: 1 addition & 1 deletion tests/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def test_run_diffbrowsers_proof_imgs(fp):
else:
imgs.append(f)
# There should be images for the text, glyphs and waterfall page
assert len(imgs) == 3
assert len(imgs) >= 3



Expand Down

0 comments on commit f4c94e4

Please sign in to comment.