python - 在 ReportLab 生成的 PDF 中包含 base64 编码的图像

标签 python django reportlab temporary-files stringio

我正在尝试解码 base64 编码的图像并将其放入我使用 ReportLab 生成的 PDF 中。我目前是这样做的(image_data 是 base64 编码的图像,story 已经是 ReportLab 的故事):

# There is some "story" I append every element
img_height = 1.5 * inch  # max image height
img_file = tempfile.NamedTemporaryFile(mode='wb', suffix='.png')
img_file.seek(0)
img_file.write(image_data.decode('base64'))
img_file.seek(0)
img_size = ImageReader(img_file.name).getSize()
img_ratio = img_size[0] / float(img_size[1])
img = Image(img_file.name,
    width=img_ratio * img_height,
    height=img_height,
)
story.append(img)

而且它有效(尽管对我来说仍然很难看)。我考虑过删除临时文件(类文件对象不应该这样做吗?)。

为了摆脱临时文件,我尝试使用 StringIO 模块,创建类文件对象并传递它而不是文件名:

# There is some "story" I append every element
img_height = 1.5 * inch  # max image height
img_file = StringIO.StringIO()
img_file.seek(0)
img_file.write(image_data.decode('base64'))
img_file.seek(0)
img_size = ImageReader(img_file).getSize()
img_ratio = img_size[0] / float(img_size[1])
img = Image(img_file,
    width=img_ratio * img_height,
    height=img_height,
)
story.append(img)

但这会给我 IOError 并显示以下消息:“无法识别图像文件”。

我知道 ReportLab 使用 PIL 来读取不同于 jpg 的图像,但是有什么方法可以避免创建命名的临时文件并且只使用类似文件的对象来执行此操作,而不将文件写入磁盘?

最佳答案

您应该用 PIL.Image.open 包装 StringIO(),因此只需 img_size = ImageReader(PIL.Image.open(img_file)).getSize()。正如 Tommaso 的回答所暗示的那样,它实际上是 Image.size 的薄包装。此外,实际上不需要自己计算 desc 大小,reportlab 的 bound 模式。Image 可以为您完成:

img_height = 1.5 * inch  # max image height
img_file = StringIO.StringIO(image_data.decode('base64'))
img_file.seek(0)
img = Image(PIL.Image.open(img_file),
            width=float('inf'),
            height=img_height,
            kind='bound')
)
story.append(img)

关于python - 在 ReportLab 生成的 PDF 中包含 base64 编码的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9980408/

相关文章:

python - 如何在 scipy 中创建数学表达式?

python - Django 1.7.3 - 字段引用的模型查找失败

django - 从基于函数的 View 更新对象值

python - 我如何保证 bool 值只对 Django 中具有特定外键的一行为真?

python - 使用 ReportLab 处理大型 PDF 所需的时间呈指数级增长

python - 使用 Django 存储/检索/编辑项目特定数据

python - 当我使用串联数据帧时,为什么会收到 ‘IndexError: string index out of range’

python : "Pandas data cast to numpy dtype of object. Check input data with np.asarray(data)."

python - 如何使用 Reportlab 在单个段落中的行之间添加空格

python - Python Reportlab 中的动态帧大小