1

Below is a simplistic example I put together to try to generate a PNG image of a barcode using python-barcode. It seems to me that no matter how I generate the barcode it always generates as an SVG. As far as I can tell this snippet follows the paradigm in the python-barcode usage guide.

When I run this and try to open the file in something like paint or any other image viewer I receive an error that this is not a valid PNG file. If I manually modify the extension to be .svg, then the image opens making me suspect it is writing in SVG format.

import barcode
from barcode.writer import ImageWriter
    
    bc_type = 'Code39' 
    options = dict(module_width=0.5,module_height=6,quiet_zone=1.0,font_size=8,text_distance=1.5,background='white',foreground='black',center_text=true, format='PNG')    
    BC= barcode.get_barcode_class(bc_type)
    
    if bc_type == 'Code39':
        with open("barcode.png", "wb") as f:
            BC('12345', writer=ImageWriter, add_checksum=false).write(f)
    else:
        with open("barcode.png", "wb") as f:
            BC('12345', writer=ImageWriter).write(f)

I have also tried to write the file using the following command instead of using a file open and write:

#with open("barcode.png", "wb") as f:
#            BC('12345', writer=ImageWriter).write(f)

bc= BC('12345', writer=ImageWriter)
file = bc.save('barcode',options)

I have tried with and without the format option set (it's supposed to default to PNG)

Lastly one thing I noticed is that in all documents I've searched for around python-barcode shows that the call line to the ImageWriter should be written as follows: bc= BC('12345', writer=ImageWriter()) with open and closed parenthesis after the class

every time I have tried to execute this way I receive an error:

'NoneType' object is not callable Traceback (most recent call last): File "Python Script", line 19, in onProcess BC('12345', writer=ImageWriter()) TypeError: 'NoneType' object is not callable

Any suggestions from the community on where I'm going wrong?

Rob
  • 13
  • 3
  • Just to make sure: you do have the `Pillow` library installed? – AKX Jun 13 '23 at 21:36
  • @AKX thank you for the suggestion. I did have pillow installed but the main issue seemed to be I was on a much older version. After upgrading I was able to move past this issue – Rob Jun 13 '23 at 22:48

1 Answers1

0

please ensure that you have the latest version of the python-barcode library installed.And try options argument should be passed as a keyword argument to the write() method, not to the save() method. So you need to include **options when calling the write() method.

import barcode
from barcode.writer import ImageWriter

type = 'Code39'  # this will be a variable
options = dict(module_width=0.5, module_height=6, quiet_zone=1.0, font_size=8, text_distance=1.5, background='white', foreground='black', center_text=True, format='PNG')

BC = barcode.get_barcode_class(type)

if type == 'Code39':
    with open("barcode.png", "wb") as f:
        BC('12345', writer=ImageWriter()).write(f, **options)
else:
    with open("barcode.png", "wb") as f:
        BC('12345', writer=ImageWriter()).write(f, **options)
  • Thank you Ramziya, this helped me get over the hump. I went and looked at the version of both Pillow and python-barcode and they were very old on this server. I installed the latest and things are much happier. With a few tweaks I have it working. One thing to note. I didn't need to use the "**" operator to pass inthe options. When I tried that I received an error: **write() got an unexpected keyword argument 'module_width'** I removed the "**" and just used this line and it appears to be working: `BC('$(Input)', writer=ImageWriter()).write(f, **options)` – Rob Jun 13 '23 at 22:46