Fix UPC barcode: use first 11 digits, not last 11

digits[-11:] was dropping the first digit of 12-digit UPCs.
digits[:11] correctly passes the first 11 digits to the barcode
library, which calculates the matching check digit.

728192558375 now encodes correctly (was 2819255837X before).
This commit is contained in:
2026-03-21 23:16:42 -07:00
parent dddfbe7355
commit 4b91ac5812

View File

@@ -204,8 +204,9 @@ def generate_barcode(upc: str, dest_dir: Path) -> Path | None:
digits = re.sub(r"\D", "", upc)
if not digits:
return None
# UPC-A needs exactly 11 digits (12th is check digit, auto-calculated)
digits = digits[-11:].zfill(11)
# UPC-A: pass first 11 digits, library auto-calculates the 12th (check digit)
# A full UPC is 12 digits where the 12th is already the check digit
digits = digits[:11].zfill(11)
try:
upc_cls = barcode.get_barcode_class("upca")
bc = upc_cls(digits, writer=ImageWriter())