利用PIL(Python Image Library)反转图片颜色
Python 3.7:
from PIL import Image
import PIL.ImageOps
#读图
image = Image.open('C:/Users/Alex/Desktop/1.png')
#反转
inverted_image = PIL.ImageOps.invert(image)
#存图
inverted_image.save('C:/Users/Alex/Desktop/2.png')
可能出现的问题:
一:无法安装PIL库
原因:PIL最新版本是 1.1.7, 最近的更新是在2009年,Python 3.x不向后兼容pip install PIL
解决方案:用Pillow代替
①pip install Pillow
②Pycharm下File-Settings-Project-Project Interpreter,点开右边的加号+,搜索Pillow,选定,Install Package
二:图片路径出错
FileNotFoundError: [Errno 2] No such file or directory
解决方案: Windows需要将 “ \ ” 改成 “ / ”
反转效果:
原图
反转图