Python Pillow:基础

目录

安装

例子1 创建图片

例子2 带背景色

例子3 颜色模式

例子4 PIL读取图片

例子5 图片找不到

例子6 如果没有扩展名

例子7 显示图片1

例子8 显示图片2


安装

pip install Pillow

例子1 创建图片

from PIL import Image

width = 400

height = 300

img = Image.new(mode="RGB",size=(width,height))

img.show()

输出

Python Pillow PIL - Create New Image

例子2 带背景色

from PIL import Image  

width = 400
height = 300

img  = Image.new( mode = "RGB", size = (width, height), color = (209, 123, 193) )
img.show()

输出

Pillow Create Image with specific color

例子3 颜色模式

from PIL import Image  

width = 400
height = 300

img  = Image.new( mode = "CMYK", size = (width, height), color = (209, 123, 193, 100) )
img.show()

输出

Pillow Create Image with specific mode

例子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()

输出

Python Pillow Show or Display Image using default GUI Program

例子8 显示图片2

from PIL import Image

im1 = Image.open("sample-image.png")

im2 = Image.open("test-image.png")

im1.show()

im2.show()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

relis

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值