WhyNotHugo / python-barcode

㊙️ Create standard barcodes with Python. No external dependencies. 100% Organic Python.
http://python-barcode.rtfd.io/
MIT License
558 stars 123 forks source link

GS1-128 images are cut-off #229

Open dsoprea opened 4 months ago

dsoprea commented 4 months ago

We're losing some pixels on the left and some pixels on the right. The payload and image are correct and match, but the number [superficially] looks off.

We're using 0.15.1 (the latest version).

We're using ImageWriter to render. There doesn't seem to be a quick solution to mitigate this given the supported parameters. Any suggestions?

image

WhyNotHugo commented 4 months ago

What class of barcode is this (so I can reproduce the example)?

I think we need to calculate the width not only based on the barcode width, but the text width too.

dsoprea commented 4 months ago

GS1-128, sorry. This seems like the simplest possible payload that I have seen for this mode/class, so maybe the extra parentheticals usually result in freeing up more space at the bottom.

dsoprea commented 4 months ago

The kludge was to slightly increase the module-width from .2 to .25:

o = {
    'module_width': .25,
}

g.write(f, options=o)

image

Incidentally, why did this affect the font size and barcode height? Note that the codes here are different but of identical length.

(before, after)

image

Obviously, the proper solution would be to update the image mechanics to use max(barcode image-size, text image-size), though I traced through the execution and could not see a straight-forward way of doing this. I tried overriding methods, return values, and module attributes, but couldn't find anything that affected the width (except for passing a module-width options to the write/render). There is probably a simple solution here that affects the overall width or ability to add extra text-padding that I missed.

Prior to updating the module-width, I figured I could always add a newline in the middle of the text, but there was also a chance of negatively affecting the payload.

Which direction would you go?

dsoprea commented 4 months ago

Also, what are "guards" and what does packed() do and/or how to use it?