I am trying to convert an Alpha-Numeric Value to PNG and then in ZPL format. Both of them have different libraries and when used independently they work fine. Look at the code:
from base64 import b64encode
from reportlab.lib import units
from reportlab.graphics import renderPM
from reportlab.graphics.barcode import createBarcodeDrawing
from reportlab.graphics.shapes import Drawing
import os
def get_barcode(value, width, barWidth = 0.05 * units.inch, fontSize = 30, humanReadable = True):
barcode = createBarcodeDrawing('Code128', value = value, barWidth = barWidth, fontSize = fontSize)
drawing_width = width
barcode_scale = drawing_width / barcode.width
drawing_height = barcode.height * barcode_scale
drawing = Drawing(drawing_width, drawing_height)
drawing.scale(barcode_scale, barcode_scale)
drawing.add(barcode, name='barcode')
return drawing
sku = [] #rough data
path = r'C:\Users\Rahul\Desktop\Barcodes'
for i in range(0,10):
sku.append('A10{}'.format(i))
for i in sku:
barcode = get_barcode(value = i, width = 600)
barcode.save(formats=['PNG'],outDir=path,fnRoot=i)
This generates barcodes in PNG Format and now the conversion to ZPL is like this:
from zplgrf import GRF
with open(r'C:\Users\Rahul\Desktop\Barcodes\A100.png','rb') as image:
grf = GRF.from_image(image.read(), 'DEMO')
grf.optimise_barcodes()
print(grf.to_zpl())
Output:
~DGR:DEMO.GRF,5400,75,:Z64:eJztlrENxSAMRI8qJSMwCpv9hM0YhRFSUnxxQelSfn0jEXQuaECW3/kMAHdsTBUfwNWQEQhEeDbEtmVHJhZXEVn2b1+ZydI8j36whr6HR3i7VIZV/ZPqXB3QsIPnsKomARznKwEKUIACFOAkWgnwdYCGFl3+JyPAtwMaVqVnQreoZnAqX60HOE6r9QANO6gZ/F2rCxESD8A=:4C36^XA^MMC,Y^PON^MNY^FO0,0^XGR:DEMO.GRF,1,1^FS^PQ1,0,0,N^XZ^XA^IDR:DEMO.GRF^FS^XZ
What I want to do is eliminate the process of saving the file and then reading it again for the conversion so I did this:
for i in sku:
barcode = get_barcode(value = i, width = 600)
barcode.save(formats=['PNG'],fnRoot=i)
print(barcode)
grf = GRF.from_image(barcode, 'DEMO')
grf.optimise_barcodes()
print(grf.to_zpl())
but I get the following error:
TypeError: a bytes-like object is required, not 'Drawing'
How can I convert the following object into "bytes-like" object??
<reportlab.graphics.shapes.Drawing object at 0x000001BF2C59DF28>
Error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-14-2b88439d6661> in <module>
3 barcode.save(formats=['PNG'],fnRoot=i)
4 print(barcode)
----> 5 grf = GRF.from_image(barcode, 'DEMO')
6 grf.optimise_barcodes()
7 print(grf.to_zpl())
~\Anaconda3\lib\site-packages\zplgrf\__init__.py in from_image(cls, image, filename)
354 """
355
--> 356 source = Image.open(BytesIO(image))
357 source = source.convert('1')
358 width = int(math.ceil(source.size[0] / 8.0))
TypeError: a bytes-like object is required, not 'Drawing'
To call GRF.from_image
, you need to pass a bytes-like object. Currently, you have saved your Drawing
as a png file, then pass that the the function. Instead, you should open the saved png file and pass the read object to from_image
like:
with open(barcode_file, "rb") as image:
grf = GRF.from_image(image.read(), "DEMO")
os.remove(barcode_file)
where barcode_file
is the path to which your Drawing
was saved. os.remove
can be used to remove the temporary file after it has been read.
To avoid creating a temporary file like this, you could use the asString
method of the Drawing
class and pass the result to from_image
:
grf = GRF.from_image(barcode.asString("png"), "DEMO")