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?