江苏省建设工程考试网站,自动推送百度wordpress,网站建设教程所需文字,微楼书网站建设PIL模块-用与记1.图片导入Image.open()2.图像显示.show()4.查看图片属性.format,.size,.mode3.图像格式转换.convert()4.图像模式“L”#xff0c;“RGB”,CYMK5. 图片旋转.rotate()旋转方式1#xff1a;旋转不扩展旋转方式2#xff1a;旋转扩展旋转方式3#…
PIL模块-用与记1.图片导入Image.open()2.图像显示.show()4.查看图片属性.format,.size,.mode3.图像格式转换.convert()4.图像模式“L”“RGB”,CYMK5. 图片旋转.rotate()旋转方式1旋转不扩展旋转方式2旋转扩展旋转方式3旋转扩展白色填充6.图片切割.crop()7.图片保存.save()PILPython Imaging Library为Python图像处理常用的库。PIL手册网站http://effbot.org/imagingbook/pil-index.htm PIL库中最重要的一个类:Image,可以通过这个类/模块 导入图像处理图像保存图像 import Image 1.图片导入Image.open() imgImage.open(“xxx/xxx/lena.jpg”) 导入路径中的文件名后要带扩展名不然会报一下错误: IOError: [Errno 2] No such file or directory: ‘lena’ 2.图像显示.show() img.show() 4.查看图片属性.format,.size,.mode
图片源格式,图片尺寸,图片模式 print(img.format , img.size , img.mode) 输出 (‘JPEG’, (200, 200), ‘RGB’) 3.图像格式转换.convert() img.convert(L) img.show() mode 从RGB 转变成L
4.图像模式“L”“RGB”,“CYMK”
PIL中的图像有一下9种模式使用.convert()函数可以让图片在这9中模式中来回转换。(作为参数模式要加引号) · 1 (1-bit pixels, black and white, stored with one pixel per byte) · L (8-bit pixels, black and white) # 灰度 · P (8-bit pixels, mapped to any other mode using a colour palette) · RGB (3x8-bit pixels, true colour) # 彩色 · RGBA (4x8-bit pixels, true colour with transparency mask) · CMYK (4x8-bit pixels, colour separation) · YCbCr (3x8-bit pixels, colour video format) · I (32-bit signed integer pixels) · F (32-bit floating point pixels) 参考资料https://www.cnblogs.com/shangpolu/p/8041848.html
5. 图片旋转.rotate()
旋转方式1旋转不扩展
#在原图上旋转,旋转后的图像与原图大小一致,转出图像的部分会被丢弃,所以会造成信息丢失
img2img.rotate(45)旋转方式2旋转扩展
#旋转后扩展信息不丢失图像尺寸变大
img3img.rotate(45,expandTrue))
旋转方式3旋转扩展白色填充
img_alphaimg.convert(RGBA) # 将原图转换成RGBA的格式,四个通道,另一个通道表示透明度,默认为255,完全不透明
rot img_alpha.rotate(45, expand True) # 旋转图片fffImage.new(RGBA,rot.size,(255,255,255,255))
outImage.composite(rot,fff,maskrot)
out.convert(img.mode).show()
# RGBA模式需要转换成RGB才能存储不然会报错参考资料https://blog.csdn.net/luolinll1212/article/details/83059118
6.图片切割.crop()
box (0, 0, 100, 150) #切割区域的四个坐标left,top,right,bottom
region img.crop(box)PIL 图像坐标系以图像的左上角为0,0位置第一维度为长第二维度为高。
7.图片保存.save() img.save(…/testdir/img.jpg) 如果文件目录不存在,就会报错 FileNotFoundError: [Errno 2] No such file or directory: ‘…/testdir/img.jpg’