我正在尝试解码 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/