0

I'm using barcode.Code39 and barcode.writer.ImageWriter from the python-barcode library (version 0.15.1) to create a simple barcode and dump it to a PNG. I was able to control the left and right borders using the quiet_zone option and remove the text using the write_text option.

However, the ImageWriter also adds top and bottom borders to the PNG created by the barcode package itself. I want to fit the image into a small space in a spreadsheet-type report. Is it possible to control these directly, rather than post-processing the PNG to remove them?

I couldn't find any related options by reading the package source code.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
OptiAndrew
  • 27
  • 6
  • 2
    Consider rephrasing / changing your question. SE mods will close opinion based questions – PatioFurnitureIsCool Sep 01 '23 at 21:15
  • For the python-barcode package, the barcode writers appear to have an undocumented attribute `margin_bottom` and `margin_top` which can be used to control vertical spacing. Can you provide a more detailed python-barcode example? See also https://github.com/WhyNotHugo/python-barcode/blob/13538c812f9c835a0787be01a49bd4a9499fa5ca/barcode/writer.py#L125 – Nick ODell Sep 01 '23 at 21:47
  • @Nick ODell I will check that. Thanks! – OptiAndrew Sep 01 '23 at 21:50
  • Welcome to Stack Overflow. Please do not edit questions to include an "update", especially not to acknowledge that an answer worked for you. You can upvote answers to approve of them, and "accept" an answer to indicate that it solved the problem. Please keep in mind that this is **not a discussion forum** - comments are not intended to be permanent, questions should contain only the question, and they should remain in a form that *best asks the question and only the question, even if* you personally no longer need it answered. – Karl Knechtel Sep 01 '23 at 23:30
  • @NickODell Unless you can find a duplicate for the question, it would be better to write that up as an answer. There is nothing fundamentally wrong with the question (or at least, I have edited to fix the most significant problems, and there is nothing here that merits closing the question now). – Karl Knechtel Sep 01 '23 at 23:31
  • 1
    @PatioFurnitureIsCool I just removed that part of the question, because it's simply unnecessary. If the best approach to the problem were to use a different library, someone answering the question could simply volunteer that information. Since it seems that the task doesn't require a different library, surely an answer that uses the existing library is satisfactory. – Karl Knechtel Sep 01 '23 at 23:38
  • Thank you all for the assistance. I don't use this forum but one in a while, so apologies that my posting etiquette may be a bit rusty. – OptiAndrew Sep 02 '23 at 00:55
  • @OptiAndrew No worries, and your answer looks fine to me. I gave it an upvote. One thing you could do to make it a better answer is to provide a short code example of how you'd use margin_top/margin_bottom. As written, it would be easy for somebody who comes along a year from now with the same problem to think that margin_top is an argument to ImageWriter's constructor. I would have written an answer w/ a code sample, but I don't have the experience with the barcode library to do that. All I know how to do is Google search really well. :) – Nick ODell Sep 02 '23 at 02:17

1 Answers1

1

python-barcode has options margin_top and margin_bottom that can control the margins.

OptiAndrew
  • 27
  • 6