fix python-barcode usage

This commit is contained in:
Piotr Oleszczyk 2025-06-18 15:09:49 +02:00
parent 85b466f482
commit 33906be0cf

26
app.py
View file

@ -22,17 +22,23 @@ app = FastAPI(title="QL-1060N Stacked Barcode Printer")
@lru_cache(maxsize=256)
def make_code128(data: str) -> Image.Image:
writer_opts = {
"module_height": 15.0,
"module_width": 0.5,
"quiet_zone": 1.0,
"font_size": 10,
"text_distance": 1.0,
}
code = Code128(
data, writer=ImageWriter(), add_checksum=False, writer_options=writer_opts
# 1) Instantiate the barcode object (checksum auto-added for Code128)
code = Code128(data, writer=ImageWriter())
# 2) Render to a PIL.Image, passing any writer options and optional text override
pil_img = code.render(
writer_options={
"module_height": 15.0, # bar height
"module_width": 0.5, # bar thickness
"quiet_zone": 1.0, # margin on each side
"font_size": 10, # text size
"text_distance": 1.0, # gap between bars and text
# you can also pass 'format': 'PNG' here if needed
},
text=data, # explicitly draw your data string under the bars
)
pil_img = code.render(writer_opts) # returns RGB
# 3) Convert to 1-bit for your Brother QL workflow
return pil_img.convert("1")