【问题标题】:How to convert an array of PIL images into a base64-formatted PDF string?如何将 PIL 图像数组转换为 base64 格式的 PDF 字符串?
【发布时间】:2018-08-02 09:38:36
【问题描述】:

下面的这个函数接受一个 JSON 字符串,里面包含一堆 base64 编码的图像。我需要这个东西来返回一个base64编码的PDF,它确实可以,但它只会给我一个页面,即JSON字符串中的第一个图像......我有这个代码:

import json
import base64
from PIL import Image
from io import BytesIO
import numpy as np
import cv2

class ImageStuff(models.TransientModel):
   _name = 'image.camera_scan'
   _description = 'Do stuff with your images'

    @api.model
    def generate_pdf_from_b64(self, b64_arr_str):
       b64_arr = json.loads(b64_arr_str)
       pil_arr, count, pil0 = [], 0, False
       for blob in b64_arr:
           b64 = blob['b64'].split(',')[1]
           pil = Image.open(BytesIO(base64.b64decode(b64)))
           if count > 0: pil_arr.append(pil)
           else: pil0 = pil
           count += 1

       in_mem_file = BytesIO()

       pil0.save(in_mem_file, format="PDF", resolution=100.0, 
                 save_all=True, append_images=pil_arr) 
                 # I think it's that line above that doesn't work

       in_mem_file.seek(0)
       img_bytes = in_mem_file.read()
       base64_encoded_result_bytes = base64.b64encode(img_bytes)
       b64_str = base64_encoded_result_bytes.decode('ascii')

       return b64_str

我是 python 的新手(我相信你可以在代码中看到)。但我想尽可能避免使用 python 2.7 不附带的模块。

我在这个东西上使用了 Odoo 10.0 API,我不能使用 pip,所以导入新模块有点痛苦。虽然,如果您认为我真的无法仅使用 PIL 来做到这一点,我仍然非常欢迎您的帮助。

【问题讨论】:

    标签: python base64 python-imaging-library odoo-10


    【解决方案1】:

    很好用,原来odoo 10的PIL模块已经过时了,需要升级一下

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-17
      • 2011-06-17
      • 1970-01-01
      • 2017-12-20
      • 2021-12-13
      • 2018-09-07
      • 2014-07-21
      • 2022-06-16
      相关资源
      最近更新 更多