知识点图片模块(Image.Image)Image模块函数Image模块方法ImageChops模块ImageColor模块基础使用图片模块Image.Image加载图片对象,旋转90度显示fromPILimportImage#displayimageim=Image.open('background.jpg')im.show()#将图片转90度im.rotate(90).show()创建缩略图128x128fromPILimportImageimportglob,ossize=128,128forinfileinglob.glob('D:\代码\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的新图片fromPILimportImageim=Image.new('RGB',(1920,1080),(255,0,0))im1=Image.new('RGB',(1920,1080),'red')im2=Image.new('RGB',(1920,1080),'#FF0000')im2.show()将图像转换为PNGim=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):逐像素比较两幅图像,返回包含较亮值的新图像fromPILimportImagefromPILimportImageChopsim1=Image.open('1.jpg')im2=Image.open('2.jpg')IC_image=ImageChops.lighter(im1,im2)IC_image.show()ImageColor模块ImageColor模块用于实现RGB颜色表转换,支持是的颜色格式包括:十六进制颜色说明符,例如,“#ff0000”指定一个纯红色RGB函数,在“rgb(red,green,blue)”中给出,其中颜色值是0到255范围内的整数,例如,“rgb(255,0,0)"和"rgb(100%,0%,0%)常见的HTML颜色名称,例如,"red"指定纯红色getrgb(color):ConvertcolorstringtoRGBtuplefromPILimportImageColorIC_image=ImageColor.getrgb('红色')print(IC_image)#(255,0,0)
