文章标题
来源:互联网 发布:程序化交易策略源码 编辑:程序博客网 时间:2024/06/15 13:44
用python简单处理图片(2)——图像通道/几何变换/裁剪
一 图像通道
1 彩色图像转灰色图
from PIL import Imageimport matplotlib.pyplot as pltimg=Image.open('/home/liu/ex.jpg')gray=img.convert('L')plt.figure("beauty")plt.imshow(gray,cmap='gray')plt.axis('off')plt.show()
使用convert函数来进行转换,它是图像实例对象的一个方法,接受一个mode函数,用于指定一种色彩模式,mode的取值可以一下几种:
- 1 (1-bit pixels,black and white,stored with one pixel per byte)
- L (8-bits 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)
2 通道分离与合并
from PIL import Imageimport matplotlib.pyplot as pltimg=Image.open('d:/ex.jpg') #打开图像gray=img.convert('L') #转换成灰度r,g,b=img.split() #分离三通道pic=Image.merge('RGB',(r,g,b)) #合并三通道plt.figure("beauty")plt.subplot(2,3,1), plt.title('origin')plt.imshow(img),plt.axis('off')plt.subplot(2,3,2), plt.title('gray')plt.imshow(gray,cmap='gray'),plt.axis('off')plt.subplot(2,3,3), plt.title('merge')plt.imshow(pic),plt.axis('off')plt.subplot(2,3,4), plt.title('r')plt.imshow(r,cmap='gray'),plt.axis('off')plt.subplot(2,3,5), plt.title('g')plt.imshow(g,cmap='gray'),plt.axis('off')plt.subplot(2,3,6), plt.title('b')plt.imshow(b,cmap='gray'),plt.axis('off')plt.show()
二 裁剪图片
从原图中裁剪感兴趣的区域,裁剪区域由4-tuple决定,该tuple中信息为(left,upper,right,lower).Pillow左边系统的原点(0,0)为图片的左上角。坐标中数字单位为像素点。
from PIL import Imageimport matplotlib.pyplot as pltimg=Image.open('d:/ex.jpg') #打开图像plt.figure("beauty")plt.subplot(1,2,1), plt.title('origin')plt.imshow(img),plt.axis('off')box=(80,100,260,300)roi=img.crop(box)plt.subplot(1,2,2), plt.title('roi')plt.imshow(roi),plt.axis('off')plt.show()
三 几何变换
image有resize(),rotate()和transpose()方法进行几何变换。
1 图像缩放和旋转
dst = img.resize((128, 128))dst = img.rotate(45) # 顺时针角度表示
2 旋转图像
dst = im.transpose(Image.FLIP_LEFT_RIGHT) #左右互换dst = im.transpose(Image.FLIP_TOP_BOTTOM) #上下互换dst = im.transpose(Image.ROTATE_90) #顺时针旋转dst = im.transpose(Image.ROTATE_180)dst = im.transpose(Image.ROTATE_270)
阅读全文
0 0
- 文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题 文章标题 文章标题 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- matlab:绘制三维曲线
- Spring学习笔记之缓存数据
- Linux搭建DOS编程环境
- 两个数之和C语言实现
- Wampserver一直是橙色的
- 文章标题
- Visual Stdio 2015打包安装项目的方法(使用Visual Studio Installer)
- Kotlin基础教程-流程控制语句
- [TED]Learning to be awesome at anything you do--Tasha Eurich
- 表格模型
- Python——RabbitMQ
- getchar() getch()
- 双向链表的建立 增删改 打印
- Unity 游戏框架搭建 (八) 减少加班利器-QLog