python pillow库的基础使用教程
知识点
- 图像模块(Image.Image)
Image模块的功能
Image模块的方法
- ImageChops模块
- ImageColor模块
基础使用
图像模块Image.Image
加载图像对象,旋转90度并显示
fromPILimportImage #显示图像 im=Image.open('background.jpg') im.show() #转换图像90度 im.rotate(90).show()
创建缩略图128x128
fromPILimportImage importglob,os size=128,128 forinfileinglob.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
fromPILimportImage 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):逐个像素地比较两个图像,并返回包含较亮值的新图像
fromPILimportImage fromPILimportImageChops 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元组
fromPILimportImageColor IC_image=ImageColor.getrgb('red') print(IC_image) # (255,0,0)
以上就是pythonpillow库的基础使用教程的详细内容,更多关于pythonpillow库使用的资料请关注毛票票其它相关文章!