【问题标题】:convert PilImage to reportlab将 PilImage 转换为 reportlab
【发布时间】:2019-02-15 10:16:16
【问题描述】:

我正在尝试在空中转换而不将临时文件从 image.pil.PilImage 对象 ('qrcode.image.pil.PilImage') 保存到 Reportlab canvas.drawImage(pilimage, x, y, width=30 , 高度=30)

我有这个错误:

TypeError: 预期 str、bytes 或 os.PathLike 对象,而不是 PilImage

之后我尝试使用 BytesIO 转换 PilImage:

io_img = BytesIO(image)

我有这个错误:

TypeError: 需要一个类似字节的对象,而不是 'PilImage'

io_img = StringIO(image)

TypeError: initial_value 必须是 str 或 None,而不是 PilImage

我发现的一切都是保存文件的解决方案并重新打开它!

有没有什么办法可以直接将PilImage对象转换成reportLab,不用保存文件再打开!

【问题讨论】:

  • 究竟什么是“PilImage”?
  • 'qrcode.image.pil.PilImage', 导入二维码, image = qrcode.make('str')
  • ReportLab 文档说“您需要 Python 图像库 (PIL) 才能将图像与 ReportLab 包一起使用”,我不知道 qrcode.make('str') 返回什么 - 所以这听起来像是您的问题的原因.
  • 好吧,qrcode.image.pil.PilImageImagePIL(或pillow)模块创建的对象的类型不同。所以你必须找到一些方法来转换它。您能读取qrcode.image.pil.PilImage 对象的各个像素吗?如果你能做到这一点,那么你应该能够自己在内存中即时转换它。
  • 感谢您的建议!我已经找到解决方案了!

标签: python image python-imaging-library qr-code reportlab


【解决方案1】:
from reportlab.lib.utils import ImageReader


pil_img = ImageReader(qrcodeclass.get_image())

canvas.drawImage(pil_img , x, y, width=30, height=30)

【讨论】:

    猜你喜欢
    • 2015-09-21
    • 2012-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-13
    • 2019-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多