知识点
- 图像模块 (image.image)
image模块的功能
image模块的方法
- imagechops模块
- imagecolor模块
基础使用
图像模块 image.image
加载图像对象,旋转90度并显示
from pil import image
#显示图像
im = image.open('background.jpg')
im.show()
# 转换图像90度
im.rotate(90).show()
创建缩略图 128x128
from pil import image
import glob, os
size = 128, 128
for infile in glob.glob('d:\code\gitee\pydata\python3-example\pillow_demo\*.jpg'):
print(infile)
filename = os.path.split(infile)[-1]
im = image.open(infile)
im.thumbnail(size, image.antialias)
im.save("d:\code\gitee\pydata\python3-example\pillow_demo\" + filename)
创建一个新图像, 分辨率为1920*1080
from pil import image
im = image.new('rgb', (1920, 1080), (255, 0, 0))
im1 = image.new('rgb', (1920, 1080), 'red')
im2 = image.new('rgb', (1920, 1080), '#ff0000')
im2.show()
将图像转换为png
im = image.open('background.jpg', 'r')
im.save('background.png')
im.show()
im_png = image.open('background.png', 'r')
print(im_png.format)
imagechops模块
imagechops模块包含多个算术图像的操作,称为通道操作,它们可以实现,特殊效果,图像合成,算法绘画等
它的功能大多数通道操作都是采用一个或两个图像参数比较来返回一个新图像,下面只列出一些常用的方法:
ic.lighter(image1,image2):逐个像素地比较两个图像,并返回包含较亮值的新图像
from pil import image
from pil import imagechops
im1=image.open('1.jpg')
im2=image.open('2.jpg')
ic_image=imagechops.lighter(im1,im2)
ic_image.show()
imagecolor模块
imagecolor模块用来实现rgb颜色表转换,它支持是颜色格式包括:
- 十六进制颜色说明符,例如,“#ff0000”指定纯红色
- rgb函数,以“rgb(红色,绿色,蓝色)”给出,其中颜色值是0到255范围内的整数,如,“rgb(255,0,0)”和“rgb(100%,0%,0%)
- 常见的html颜色名称,例如,“red”指定纯红色
getrgb(color):将颜色字符串转换为rgb元组
from pil import imagecolor
ic_image=imagecolor.getrgb('red')
print(ic_image)
#
(255, 0, 0)
以上就是python pillow库的基础使用教程的详细内容,更多关于python pillow库使用的资料请关注其它相关文章!
_包皮卷蛆蘸屎吃_