复制图像时使用PIL保留颜色

时间:2015-07-13 17:12:21

标签: python django django-views python-imaging-library

我有这段代码

# Creating QR
img = None
img_io = StringIO.StringIO()

img = qrcode.make('http://my-domain.com/scanned/'+str(s.pk))
img_w, img_h = img.size

logo = Image.open(settings.MEDIA_ROOT+'logo.png', 'r')
logo_w, logo_h = logo.size

offset = ((img_w - logo_w) / 2, (img_h - logo_h) / 2)
img.paste(logo, offset)

img.save(img_io)
qr_code = InMemoryUploadedFile(img_io, None, 'the-qr.jpg', 'image/jpeg', img_io.len, None)

图片' logo.png'是红色,但是当我保存最终图像全部是黑白时,我该如何保留RGB颜色?

我试过这个但是引发了错误:

img.convert('RGB').save(img_io)


File "/var/www/project/api/views.py", line 361, in perform_update
    img.convert('RGB').save(img_io)
  File "/usr/local/lib/python2.7/dist-packages/PIL/Image.py", line 1687, in save
    format = EXTENSION[ext]
KeyError: ''

0 个答案:

没有答案