图像通道/几何变换/裁剪

一 图像通道

1 彩色图像转灰度图

在转成灰度图之前,我们先来看看我们的彩色图像。

from PIL import Image
import matplotlib.pyplot as plt
img = Image.open("D:\\杨幂.jpg")
plt.figure("美女")
plt.imshow(img)
plt.axis("off")
plt.show()
print(img.size)   #图片的大小
print(img.mode)   #图片的模式
print(img.format) #图片的格式

输出结果如下所示:

(480, 502)
RGBA
PNG

然后转成灰度图片:

from PIL import Image
import matplotlib.pyplot as plt
img = Image.open("D:\\杨幂.jpg")
gray = img.convert("L")
plt.figure("美女")
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-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)

2、通道分离与合并

from PIL import Image
import matplotlib.pyplot as plt
img = Image.open("D:\\杨幂.jpg")
gray = img.convert("L")
r,g,b,a = img.split()
pic = Image.merge("RGB",(r,g,b))
plt.figure("美女")
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()

结果如下图所示:

二 裁剪图片

从原图片中裁剪感兴趣区域(roi),裁剪区域由4-tuple决定,该tuple中信息为(起始点横坐标 ,起始点纵坐标, 宽度, 高度)。 Pillow左边系统的原点(0,0)为图片的左上角。坐标中的数字单位为像素点。

from PIL import Image
import matplotlib.pyplot as plt
img = Image.open("D:\\杨幂.jpg")
plt.figure("美女")
plt.subplot(1,2,1),plt.title("origin")
plt.imshow(img),plt.axis("off")

box = (45,35,230,260)
roi = img.crop(box)
plt.subplot(1,2,2),plt.title("ori")
plt.imshow(roi),plt.axis("off")
plt.show()

结果如下所示:

三 几何变换

Image类有resize()、rotate()和transpose()方法进行几何变换。

图像的缩放

from PIL import Image
import matplotlib.pyplot as plt
img = Image.open("D:\\杨幂.jpg")
dst = img.resize((128,128))
plt.figure("刘小辉证件照")
plt.imshow(dst),plt.axis("off")
plt.show()

结果如下所示:

图像的旋转

from PIL import Image
import matplotlib.pyplot as plt
img = Image.open("D:\\杨幂.jpg")
dst1 = img.rotate(45) # 图像逆时针45度旋转
dst2 = img.rotate(-45) # 图像顺时针45度旋转
plt.figure("美女")
plt.subplot(1,3,1),plt.title("origin")
plt.imshow(img),plt.axis("off")
plt.subplot(1,3,2),plt.title("anticlockwise")
plt.imshow(dst1),plt.axis("off")
plt.subplot(1,3,3),plt.title("clockwise")
plt.imshow(dst2),plt.axis("off")
plt.show()

结果如下图所示:

图像转换

左右互换

from PIL import Image
import matplotlib.pyplot as plt
img = Image.open("D:\\杨幂.jpg")
dst = img.transpose(Image.FLIP_LEFT_RIGHT) #左右互换
plt.figure("美女")
plt.imshow(dst),plt.axis("off")
plt.show()

结果如下:

上下互换

from PIL import Image
import matplotlib.pyplot as plt
img = Image.open("D:\\杨幂.jpg")
dst = img.transpose(Image.FLIP_TOP_BOTTOM) #上下互换
plt.figure("美女")
plt.imshow(dst),plt.axis("off")
plt.show()

结果如下:

旋转的另一种表达形式

from PIL import Image
import matplotlib.pyplot as plt
img = Image.open("D:\\杨幂.jpg")
dst1 = img.transpose(Image.ROTATE_90)  #逆时针旋转90度
dst2 = img.transpose(Image.ROTATE_180)
dst3 = img.transpose(Image.ROTATE_270)
plt.figure("美女")
plt.subplot(2,2,1),plt.title("0")
plt.imshow(img),plt.axis("off")
plt.subplot(2,2,2),plt.title("90")
plt.imshow(dst1),plt.axis("off")
plt.subplot(2,2,3),plt.title("180")
plt.imshow(dst2),plt.axis("off")
plt.subplot(2,2,4),plt.title("270")
plt.imshow(dst3),plt.axis("off")
plt.show()

结果如下图所示:

注:transpose()和rotate()没有性能差别。

参考:https://www.cnblogs.com/denny402/p/5096330.html

猜你喜欢

转载自www.cnblogs.com/Terrypython/p/9936898.html