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