决定在jupyter笔记本中尝试PIL库。我有一张蓝色(没有其他颜色)的png格式图片。
想要使其半透明,于是我执行了以下操作:
from PIL import Image
blue = Image.open("blue_color.png")
当我通过Jupyter打开图像时,一切都很正常。但是当我应用.putalpha()方法时:
blue.putalpha(128)
更让人惊喜的是:
KeyError Traceback (most recent call last)
~\Anaconda3\lib\site-packages\PIL\PngImagePlugin.py in _save(im, fp, filename, chunk)
799 try:
--> 800 rawmode, mode = _OUTMODES[mode]
801 except KeyError:
KeyError: 'PA'
During handling of the above exception, another exception occurred:
OSError Traceback (most recent call last)
~\Anaconda3\lib\site-packages\IPython\core\formatters.py in __call__(self, obj)
343 method = get_real_method(obj, self.print_method)
344 if method is not None:
--> 345 return method()
346 return None
347 else:
~\Anaconda3\lib\site-packages\PIL\Image.py in _repr_png_(self)
698 """
699 b = io.BytesIO()
--> 700 self.save(b, "PNG")
701 return b.getvalue()
702
~\Anaconda3\lib\site-packages\PIL\Image.py in save(self, fp, format, **params)
2082
2083 try:
-> 2084 save_handler(self, fp, filename)
2085 finally:
2086 # do what we can to clean up
~\Anaconda3\lib\site-packages\PIL\PngImagePlugin.py in _save(im, fp, filename, chunk)
800 rawmode, mode = _OUTMODES[mode]
801 except KeyError:
--> 802 raise IOError("cannot write mode %s as PNG" % mode)
803
804 #
OSError: cannot write mode PA as PNG
我用同样的操作处理了另一个颜色文件,但它是jpg格式。一切正常!
这是文件格式的问题吗?有人可以告诉我如何解决吗?
提前感谢!
blue = Image.open("blue_color.png").convert('RGB')
,并阅读此处内容... https://dev59.com/PlQK5IYBdhLWcg3wL9Bq#52307690 - Mark Setchell