Python错误集锦:amzqr用RGBA背景图片生成jpg格式文件时报错:cannot write mode RGBA as JPEG

原文链接:http://www.juzicode.com/archives/6316

 

错误提示:

 amzqr(MyQR)用RGBA背景图片生成jpg格式文件时报错:cannot write mode RGBA as JPEG

#VX公众号: 桔子code; juzicode.com
from amzqr import amzqr
version, level, qr_name = amzqr.run(
    words='http://weixin.qq.com/r/Ejr54d-EkYLurZuC928A',
    version=5,
    level='H',
    picture='logo.jpg',
    colorized=True,
    contrast=1.0,
    brightness=1.0,
    save_name='x.jpg',
    save_dir=os.getcwd()
) 
print(version, level, qr_name)
==========运行结果:
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
d:\python\python38\lib\site-packages\PIL\JpegImagePlugin.py in _save(im, fp, filename)
    611     try:
--> 612         rawmode = RAWMODE[im.mode]
    613     except KeyError:

KeyError: 'RGBA'

During handling of the above exception, another exception occurred:

OSError                                   Traceback (most recent call last)
<ipython-input-37-083600492a3d> in <module>
      1 #VX公众号: 桔子code; juzicode.com
      2 from amzqr import amzqr
----> 3 version, level, qr_name = amzqr.run(
      4     words='http://weixin.qq.com/r/Ejr54d-EkYLurZuC928A' class="ansi-yellow-intense-fg ansi-bold">,
      5     version=5,

d:\python\python38\lib\site-packages\amzqr\amzqr.py in run(words, version, level, picture, colorized, contrast, brightness, save_name, save_dir)
    117             imageio.mimwrite(qr_name, ims, '.gif', **{ 'duration': duration/1000 })
    118         elif picture:
--> 119             qr_name = combine(ver, qr_name, picture, colorized, contrast, brightness, save_dir, save_name)
    120         elif qr_name:
    121             qr = Image.open(qr_name)

d:\python\python38\lib\site-packages\amzqr\amzqr.py in combine(ver, qr_name, bg_name, colorized, contrast, brightness, save_dir, save_name)
     83 
     84         qr_name = os.path.join(save_dir, os.path.splitext(os.path.basename(bg_name))[0] + '_qrcode.png') if not save_name else os.path.join(save_dir, save_name)
---> 85         qr.resize((qr.size[0]*3, qr.size[1]*3)).save(qr_name)
     86         return qr_name
     87 

d:\python\python38\lib\site-packages\PIL\Image.py in save(self, fp, format, **params)
   2132 
   2133         try:
-> 2134             save_handler(self, fp, filename)
   2135         finally:
   2136             # do what we can to clean up

d:\python\python38\lib\site-packages\PIL\JpegImagePlugin.py in _save(im, fp, filename)
    612         rawmode = RAWMODE[im.mode]
    613     except KeyError:
--> 614         raise OSError("cannot write mode %s as JPEG" % im.mode)
    615 
    616     info = im.encoderinfo

OSError: cannot write mode RGBA as JPEG
​

 

错误原因:

1、amzqr的pictrure参数所使用的图片为RGBA格式时,save_name保存的文件格式不能是jpg,必须是bmp或png。

 

解决方法:

1、save_name参数保存的文件格式改为png或者bmp

#VX公众号: 桔子code; juzicode.com
from amzqr import amzqr
version, level, qr_name = amzqr.run(
    words='http://weixin.qq.com/r/Ejr54d-EkYLurZuC928A',
    version=5,
    level='H',
    picture='logo.jpg',
    colorized=True,
    contrast=1.0,
    brightness=1.0,
    save_name='x.png', #改为png格式
    save_dir=os.getcwd()
) 
print(version, level, qr_name)
==========运行结果:
line 16: mode: byte 
5 H E:\juzicode\x.png

 

扩展内容:

  1. 一行代码深度定制你的专属二维码:(amzqr、MyQR制作动态二维码)

 


 

如果本文还没有完全解决你的疑惑,你也可以在微信公众号“桔子code”后台给我留言,欢迎一起探讨交流。

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注