打开/保存图片
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
img = Image.open("test.jpg")
img.save("test.png")
img.save("test.bmp")
img1 = Image.open("test.png")
img2 = Image.open("test.bmp")
print("image:", img.format)
print("image1:", img1.format)
print("image2:", img2.format)
print("size:", img.size)
print("mode", img.mode)
plt.figure(figsize=(15, 5))
plt.subplot(131)
plt.axis("off")
plt.imshow(img)
plt.title(img.format)
plt.subplot(132)
plt.axis("off")
plt.imshow(img1)
plt.title(img1.format)
plt.subplot(133)
plt.axis("off")
plt.imshow(img2)
plt.title(img2.format)
plt.show()
转换图像的色彩模式
img.convert()
from PIL import Image
import matplotlib.pyplot as plt
img = Image.open("test.jpg")
img_gray = img.convert("L")
print("mode=", img_gray.mode)
plt.figure(figsize=(5, 5))
plt.imshow(img_gray)
plt.show()
img_gray.save("img_gray.bmp")
颜色通道的分离与合并
from PIL import Image
import matplotlib.pyplot as plt
img = Image.open("test.jpg")
img_r, img_g, img_b = img.split()
plt.figure(figsize=(10, 10))
plt.subplot(221)
plt.axis("off")
plt.imshow(img_r, cmap="gray")
plt.title(img_r.format, fontsize=20)
plt.subplot(222)
plt.axis("off")
plt.imshow(img_g, cmap="gray")
plt.title(img_g.format, fontsize=20)
plt.subplot(223)
plt.axis("off")
plt.imshow(img_b, cmap="gray")
plt.title(img_b.format, fontsize=20)
img_rgb = Image.merge("RGB", [img_r, img_g, img_b])
plt.subplot(224)
plt.axis("off")
plt.imshow(img_rgb)
plt.title("RGB", fontsize=20)
plt.show()
图像转数组
可以直接用numpy函数转换
import numpy as np
from PIL import Image
img = Image.open("test.jpg")
arr_img = np.array(img)
print("shape:", arr_img.shape, "\n")
print(arr_img)
图像反色处理
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
img = Image.open("test.jpg")
img_gray = img.convert("L")
arr_img = np.array(img_gray)
arr_img_new = 255 - arr_img
plt.figure(figsize=(10, 5))
plt.subplot(121)
plt.axis("off")
plt.imshow(arr_img, cmap="gray")
plt.subplot(122)
plt.axis("off")
plt.imshow(arr_img_new, cmap="gray")
plt.show()
图像缩放
使用图像对象.resize((width, height)) 来改变图像尺寸 或者使用图像对象.thumbnail((width, height)) ,该方法则是直接作用于图像对象,原地操作
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
plt.figure(figsize=(5, 5))
img = Image.open("test.jpg")
img_small = img.resize((64, 64))
plt.imshow(img_small)
plt.show()
图像旋转、镜像
图片对象.transpose(旋转方式)
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = "SimHei"
img = Image.open("test.jpg")
plt.figure(figsize=(10, 10))
plt.subplot(221)
plt.axis("off")
plt.imshow(img)
plt.title("原图", fontsize=20)
plt.subplot(222)
plt.axis("off")
img_flr = img.transpose(Image.FLIP_LEFT_RIGHT)
plt.imshow(img_flr)
plt.title("左右翻转", fontsize=20)
plt.subplot(223)
plt.axis("off")
img_r90 = img.transpose(Image.ROTATE_90)
plt.imshow(img_r90)
plt.title("逆时针旋转90度", fontsize=20)
plt.subplot(224)
plt.axis("off")
img_tp = img.transpose(Image.TRANSPOSE)
plt.imshow(img_tp)
plt.title("转置", fontsize=20)
plt.show()
图片裁剪
图片对象.crop(左上角x,左上角y,右上角x,右上角y)
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
img = Image.open("test.jpg")
plt.figure(figsize=(10, 5))
plt.subplot(121)
plt.imshow(img)
plt.subplot(122)
img_region = img.crop((100, 100, 1500, 1500))
plt.imshow(img_region)
plt.show()
|