python - TypeError:需要一个类似字节的对象,而不是“绘图”
问题描述
我正在尝试将字母数字值转换为 PNG,然后转换为 ZPL 格式。它们都有不同的库,当独立使用时,它们可以正常工作。看代码:
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)
这会生成 PNG 格式的条形码,现在转换为 ZPL 是这样的:
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())
输出:
~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
我想要做的是消除保存文件然后再次读取它以进行转换的过程,所以我这样做了:
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())
但我收到以下错误:
TypeError:需要一个类似字节的对象,而不是“绘图”
如何将以下对象转换为“类似字节”的对象?
<reportlab.graphics.shapes.Drawing object at 0x000001BF2C59DF28>
错误:
---------------------------------------------------------------------------
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'
解决方案
要调用GRF.from_image
,您需要传递一个类似字节的对象。目前,您已将您的文件保存Drawing
为 png 文件,然后将其传递给该函数。相反,您应该打开保存的 png 文件并将读取的对象传递给from_image
like:
with open(barcode_file, "rb") as image:
grf = GRF.from_image(image.read(), "DEMO")
os.remove(barcode_file)
你的保存barcode_file
路径在哪里。可用于在读取临时文件后删除它。Drawing
os.remove
为避免创建这样的临时文件,您可以使用类的asString
方法Drawing
并将结果传递给from_image
:
grf = GRF.from_image(barcode.asString("png"), "DEMO")
推荐阅读
- javascript - 单击时的多个功能可防止带有事件参数的功能-Vue
- javascript - 如何在 React styled-components 的模板文字中定义的单个回调中使用多个道具
- python - Tensorflow Github 源代码中的 Softmax 交叉熵实现
- wpf - 添加 ResourceDirectory 时出现不明确的错误消息
- android - 如何在 Xamarin 项目中有效地添加依赖 .aar 文件?
- javascript - 如何从浏览器中的本地 Apache 网络服务器正确加载 javascript 和样式?
- python - 在 Windows 上使用 Github Actions 进行 Python 测试
- javascript - 跨域请求被 cors 阻止
- sql - SQL查询在单个查询中检索不同聚合类型的平均值
- html - 如何最小化 ngx-bootstrap 手风琴盒?