pip install pillow
练习如下:
# 图像处理/展现的相关函数库 import matplotlib.pyplot as plt import warnings warnings.filterwarnings('ignore') import os from PIL import Image # 测试用的图像 test_image = 'D:/bg22.jpg' '''# 载入图像 image = Image.open(test_image) # 存储图像并转换格式(jpg->png) image.save('D:/bgafter.png') # 载入图像 image = Image.open(test_image) # 调整大小 new_image = image.resize((200,100)) print("原图像的大小:",image.size) print("新图像的大小:",new_image.size) plt.imshow(new_image) plt.show() plt.imshow(image) plt.show() # 载入图像 im = Image.open(test_image) im.thumbnail((200, 200)) print(im.size) plt.imshow(im) plt.show() ''' # 载入图像 image = Image.open(test_image) # 定义要裁剪的边界框坐标 x1 = 0 y1 = 50 x2 = 460 y2 = 320 bbox = (x1,y1,x2,y2) # 进行裁剪 cropped_image = image.crop(bbox) plt.imshow(cropped_image) plt.show() # 载入图像 image = Image.open(test_image) # 逆时针地旋转图像90度 image_rot_90 = image.rotate(90) plt.imshow(image_rot_90) plt.show() # 逆时针地旋转图像180度 image_rot_180 = image.rotate(120) plt.imshow(image_rot_180) plt.show() # 载入图像 image = Image.open(test_image) # 左右互换 image_flip = image.transpose(Image.FLIP_LEFT_RIGHT) plt.imshow(image_flip) plt.show() # 上下互换 image_flip = image.transpose(Image.FLIP_TOP_BOTTOM) plt.imshow(image_flip) plt.show() # 载入图像 image = Image.open(test_image) # 将彩色转换成灰阶 greyscale_image = image.convert('L') # 注意要注明cmap='gray'才能够正确秀出灰阶图像 plt.imshow(greyscale_image,cmap='gray') plt.show() # 载入图像 image = Image.open(test_image) # 将彩色转换成灰阶 greyscale_image = image.convert('L') # 注意要注明cmap='gray'才能够正确秀出灰阶图像 plt.imshow(greyscale_image,cmap='gray') plt.show()