Skip to content

Commit 19acfdd

Browse files
committed
Make output dir optional
1 parent c29573a commit 19acfdd

7 files changed

Lines changed: 36 additions & 15 deletions

File tree

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Usage: diffpdf [OPTIONS] REFERENCE ACTUAL
3333
Options:
3434
--threshold FLOAT Pixelmatch threshold (0.0-1.0)
3535
--dpi INTEGER Render resolution
36-
--output-dir DIRECTORY Diff image output directory
36+
--output-dir DIRECTORY Diff image output directory (optional, if not specified no diff images are saved)
3737
-v, --verbose Increase verbosity (-v for INFO, -vv for DEBUG)
3838
--save-log Write log output to log.txt
3939
--version Show the version and exit.
@@ -51,7 +51,10 @@ Options:
5151
```python
5252
from diffpdf import diffpdf
5353

54+
# Basic usage (no diff images saved)
5455
diffpdf("reference.pdf", "actual.pdf")
56+
57+
# With options (save diff images to ./output directory)
5558
diffpdf("reference.pdf", "actual.pdf", output_dir="./output", threshold=0.2, dpi=150, verbosity=2)
5659
```
5760

src/diffpdf/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def diffpdf(
1212
actual: str | Path,
1313
threshold: float = 0.1,
1414
dpi: int = 96,
15-
output_dir: str | Path = "./",
15+
output_dir: str | Path | None = None,
1616
verbosity: int = 0,
1717
save_log: bool = False,
1818
) -> None:

src/diffpdf/cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
@click.option(
2020
"--output-dir",
2121
type=click.Path(file_okay=False, path_type=Path),
22-
default="./",
23-
help="Diff image output directory",
22+
default=None,
23+
help="Diff image output directory (if not specified, no diff images are saved)",
2424
)
2525
@click.option(
2626
"-v",

src/diffpdf/comparators.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99

1010
def compare_pdfs(
11-
ref: Path, actual: Path, threshold: float, dpi: int, output_dir: Path, logger
11+
ref: Path, actual: Path, threshold: float, dpi: int, output_dir: Path | None, logger
1212
) -> None:
1313
check_hash(ref, actual, logger)
1414

src/diffpdf/visual_check.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ def render_page_to_image(pdf_path: Path, page_num: int, dpi: int) -> Image.Image
1616

1717

1818
def compare_images(
19-
ref_img: Image.Image, actual_img: Image.Image, threshold: float, output_path: Path
19+
ref_img: Image.Image,
20+
actual_img: Image.Image,
21+
threshold: float,
22+
output_path: Path | None,
2023
) -> bool:
2124
mismatch_count = pixelmatch(
2225
ref_img, actual_img, diff_path=output_path, threshold=threshold
@@ -29,11 +32,12 @@ def compare_images(
2932

3033

3134
def check_visual_content(
32-
ref: Path, actual: Path, threshold: float, dpi: int, output_dir: Path, logger
35+
ref: Path, actual: Path, threshold: float, dpi: int, output_dir: Path | None, logger
3336
) -> None:
3437
logger.info("[4/4] Checking visual content...")
3538

36-
output_dir.mkdir(parents=True, exist_ok=True)
39+
if output_dir is not None:
40+
output_dir.mkdir(parents=True, exist_ok=True)
3741

3842
ref_doc = fitz.open(ref)
3943
page_count = len(ref_doc)
@@ -45,11 +49,13 @@ def check_visual_content(
4549
ref_img = render_page_to_image(ref, page_num, dpi)
4650
actual_img = render_page_to_image(actual, page_num, dpi)
4751

48-
ref_name = ref.stem
49-
actual_name = actual.stem
50-
output_path = (
51-
output_dir / f"{ref_name}_vs_{actual_name}_page{page_num + 1}_diff.png"
52-
)
52+
output_path = None
53+
if output_dir is not None:
54+
ref_name = ref.stem
55+
actual_name = actual.stem
56+
output_path = (
57+
output_dir / f"{ref_name}_vs_{actual_name}_page{page_num + 1}_diff.png"
58+
)
5359

5460
passed = compare_images(ref_img, actual_img, threshold, output_path)
5561

tests/test_api.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@
77
TEST_ASSETS_DIR = Path(__file__).parent / "assets"
88

99

10-
def test_diffpdf_function():
10+
def test_diffpdf():
1111
with pytest.raises(SystemExit) as exc_info:
1212
diffpdf(
1313
TEST_ASSETS_DIR / "pass/identical-A.pdf",
1414
TEST_ASSETS_DIR / "pass/identical-B.pdf",
15-
output_dir=Path("./"),
1615
)
1716
assert exc_info.value.code == 0

tests/test_comparators.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,16 @@ def test_comparators(ref_pdf_rel, actual_pdf_rel, expected_exit_code):
3333
result = runner.invoke(cli, [ref_pdf, actual_pdf])
3434

3535
assert result.exit_code == expected_exit_code
36+
37+
38+
def test_comparators_with_output_dir():
39+
runner = CliRunner()
40+
41+
with runner.isolated_filesystem():
42+
ref_pdf = str(TEST_ASSETS_DIR / "fail/major-color-diff-A.pdf")
43+
actual_pdf = str(TEST_ASSETS_DIR / "fail/major-color-diff-B.pdf")
44+
45+
result = runner.invoke(cli, [ref_pdf, actual_pdf, "--output-dir", "./diff"])
46+
47+
assert result.exit_code == 1
48+
assert Path("./diff").exists()

0 commit comments

Comments
 (0)