MCPcopy Create free account
hub / github.com/bgeron/diff-pdf-visually / imgdiff

Function imgdiff

diff_pdf_visually/diff.py:51–80  ·  view source on GitHub ↗
(a, b, diff, log, print_cmds)

Source from the content-addressed store, hash-verified

49
50# returns a float, which can be inf
51def imgdiff(a, b, diff, log, print_cmds):
52 assert a.is_file()
53 assert b.is_file()
54 assert not diff.exists()
55 assert not log.exists()
56
57 with log.open("w+b") as f:
58 cmdresult = verbose_run(
59 print_cmds,
60 external_programs.compare_cmd(print_cmds)
61 + ["-verbose", "-metric", "PSNR", str(a), str(b), str(diff),],
62 stdout=f,
63 stderr=subprocess.STDOUT,
64 )
65
66 if cmdresult.returncode > 1:
67 raise ValueError("compare crashed, status=" + str(cmdresult.returncode))
68
69 with log.open("r") as f:
70 lines = f.readlines()
71
72 if any("image widths or heights differ" in l for l in lines):
73 raise ValueError("image widths or heights differ")
74
75 PREF = " all: "
76 all_line = [l for l in lines if l.startswith(PREF)]
77 assert len(all_line) == 1
78 all_str = all_line[0][len(PREF) :].strip()
79 all_num = INFINITY if (all_str == "0" or all_str == "1.#INF") else float(all_str)
80 return all_num
81
82
83

Callers 1

pdfdiff_pagesFunction · 0.85

Calls 1

verbose_runFunction · 0.85

Tested by

no test coverage detected