目录
安装
pip install Pillow
例子1 创建图片
from PIL import Image
width = 400
height = 300
img = Image.new(mode="RGB",size=(width,height))
img.show()
输出
例子2 带背景色
from PIL import Image
width = 400
height = 300
img = Image.new( mode = "RGB", size = (width, height), color = (209, 123, 193) )
img.show()
输出
例子3 颜色模式
from PIL import Image
width = 400
height = 300
img = Image.new( mode = "CMYK", size = (width, height), color = (209, 123, 193, 100) )
img.show()
输出
例子4 PIL读取图片
from PIL import Image
im = Image.open("D:/sample-image.png)
例子5 图片找不到
from PIL import Image
im = Image.open("D:/images/no-image.png")
输出
Traceback (most recent call last):
File "d:/workspace/example.py", line 3, in <module>
im = Image.open("D:/images/sample-image.png")
File "C:\Users\pythonexamplesorg\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\Image.py", line 2652, in open
fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'D:/images/sample-image.png'
例子6 如果没有扩展名
from PIL import Image
im = Image.open("D:/sample")
可以的。
例子7 显示图片1
from PIL import Image
im=Image.open("sample-image.png")
im.show()
输出
例子8 显示图片2
from PIL import Image
im1 = Image.open("sample-image.png")
im2 = Image.open("test-image.png")
im1.show()
im2.show()