首页 > 解决方案 > 图像不匹配 pil,django 模型

问题描述

我正在尝试在我的 Django 模型中创建一个 QR 码,它会在保存新记录时自行创建。问题是,在我的开发环境中进行测试时,一切都很好,但在生产中,我在下面的指定行上收到错误消息:ValueError,图像不匹配。

from django.core.files import File

import qrcode
from io import BytesIO
from PIL import Image

from datetime import date

# this is the save method of the django model class, I just left out the other methods
def save(self, *args, **kwargs):
    img = qrcode.make(self.account_id)
    canvas = Image.new('RGB', (400, 400), 'white')
    canvas.paste(img) # Error occurs on this line
    buffer = BytesIO()
    canvas.save(buffer, 'PNG')
    canvas.close()
    fname = f'pdf-qr-code-{self.account_id}.png'
    self.menu_qr_code.save(fname, File(buffer), save=False)

    super(PDFMenu, self).save(*args, **kwargs)

我不知道问题是否出在我的 ubuntu 服务器上的配置上,因为这是问题发生的地方,但可能需要一种在此自定义保存方法中创建二维码的新方法。

在此先感谢您的帮助。

标签: pythondjango

解决方案


推荐阅读