Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/publish-pypi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ jobs:
name: python-package-distributions
path: dist/
- name: Publish to PyPI
uses: pypa/gh-action-pypi-publish@cef221092ed1bacb1cc03d23a2d87d1d172e277b # release/v1
uses: pypa/gh-action-pypi-publish@ba38be9e461d3875417946c167d0b5f3d385a247 # release/v1
18 changes: 9 additions & 9 deletions src/pdf_form_tools/pdf_form_overlay.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from dataclasses import dataclass
from functools import lru_cache
from io import BytesIO
from itertools import pairwise
from pathlib import Path

import cv2
Expand All @@ -17,7 +18,6 @@
from reportlab.lib.utils import ImageReader
from reportlab.pdfgen import canvas


TEXT_COLOR = (20, 20, 20, 255)
WINDOWS_FONT_DIR = Path(os.environ["WINDIR"]) / "Fonts" if "WINDIR" in os.environ else None

Expand Down Expand Up @@ -61,7 +61,7 @@ def x2(self) -> int:
def y2(self) -> int:
return self.y + self.h

def inset(self, dx: int, dy: int | None = None) -> "Rect":
def inset(self, dx: int, dy: int | None = None) -> Rect:
if dy is None:
dy = dx
return Rect(self.x + dx, self.y + dy, self.w - dx * 2, self.h - dy * 2)
Expand All @@ -73,7 +73,7 @@ def band(
height: int,
left_pad: int = 0,
right_pad: int | None = None,
) -> "Rect":
) -> Rect:
if right_pad is None:
right_pad = left_pad
return Rect(self.x + left_pad, self.y + top_pad, self.w - left_pad - right_pad, height)
Expand All @@ -85,7 +85,7 @@ def above(
gap: int = 0,
left_pad: int = 0,
right_pad: int | None = None,
) -> "Rect":
) -> Rect:
if right_pad is None:
right_pad = left_pad
return Rect(self.x + left_pad, self.y - gap - height, self.w - left_pad - right_pad, height)
Expand Down Expand Up @@ -302,7 +302,7 @@ def detect_id_slots(page_gray: np.ndarray, rect: Rect) -> list[Rect]:

boundaries = [0]
for left, right in ranges:
center = int(round((left + right) / 2))
center = round((left + right) / 2)
if 4 < center < crop.shape[1] - 5:
boundaries.append(center)
boundaries.append(crop.shape[1] - 1)
Expand All @@ -314,7 +314,7 @@ def detect_id_slots(page_gray: np.ndarray, rect: Rect) -> list[Rect]:
digit_top = rect.y + first_guide_row - int(rect.h * 0.34)
digit_height = int(rect.h * 0.48)
slots: list[Rect] = []
for left, right in zip(boundaries, boundaries[1:]):
for left, right in pairwise(boundaries):
slots.append(Rect(rect.x + left + 3, digit_top, right - left - 6, digit_height))
return slots

Expand Down Expand Up @@ -362,16 +362,16 @@ def paste_signature(
if alpha_bbox:
signature = signature.crop(alpha_bbox)

min_signature_width = int(round((overlay.width / 21.0) * min_cm_width))
min_signature_width = round((overlay.width / 21.0) * min_cm_width)
target_width = min(line_rect.w, max(min_signature_width, int(line_rect.w * 0.55)))
width_scale = target_width / signature.width
if target_height is None:
scale = width_scale
else:
scale = min(width_scale, target_height / signature.height)

resized_width = max(1, int(round(signature.width * scale)))
resized_height = max(1, int(round(signature.height * scale)))
resized_width = max(1, round(signature.width * scale))
resized_height = max(1, round(signature.height * scale))
resized = signature.resize((resized_width, resized_height), Image.Resampling.LANCZOS)
x = int(line_rect.x + (line_rect.w - resized_width) / 2)
y = int(line_rect.y - resized_height + y_offset)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_pdf_form_overlay.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from pathlib import Path

import numpy as np
import pdf_form_tools.pdf_form_overlay as overlay
from PIL import Image, ImageDraw
from pypdf import PdfReader
from reportlab.pdfgen import canvas

import pdf_form_tools.pdf_form_overlay as overlay
from pdf_form_tools import Rect, centered_address_box, detect_id_slots


Expand Down