I am trying to use viivakoodi libary here to generate barcode from a string. I want to read this data in memory and not write it to file. Is there a way to do so ?
import barcode
EAN = barcode.get_barcode_class('ean13')
ean = EAN(u'5901234123457', writer=ImageWriter())
After I run this in python, I am getting following in ean object after printing ean.writer.__dict__
{'_callbacks': {'finish': <bound method ImageWriter._finish of <barcode.writer.ImageWriter object at 0x7f0894072d90>>,
'initialize': <bound method ImageWriter._init of <barcode.writer.ImageWriter object at 0x7f0894072d90>>,
'paint_module': <bound method ImageWriter._paint_module of <barcode.writer.ImageWriter object at 0x7f0894072d90>>,
'paint_text': <bound method ImageWriter._paint_text of <barcode.writer.ImageWriter object at 0x7f0894072d90>>},
'_draw': None,
'_image': None,
'background': u'white',
'center_text': True,
'dpi': 300,
'font_size': 10,
'foreground': u'black',
'format': u'PNG',
'module_height': 10,
'module_width': 10,
'quiet_zone': 6.5,
'text': u'',
'text_distance': 5}
Not sure how I can read the bytes that will be written to file in a variable!
Following writes bytes to file but I want to read it in memory instead of writing:
ean.save('ean13_barcode')
The library's examples mention you can create a (file-like but in-memory) StringIO object, and write the ean into it.
import barcode
from StringIO import StringIO # Python 2
from io import StringIO # Python 3
EAN = barcode.get_barcode_class('ean13')
ean = EAN(u'5901234123457', writer=ImageWriter())
fp = StringIO()
ean.write(fp)
Then you can call ean.getvalue()
to access the object's value.