from PIL import Image
import matplotlib.pyplot as plt#绘图库
import random#随机数库
#打开图像
img = Image.open('2007_000032.jpg')#读取图像
label= Image.open( '2007_000032.png')#读取标签
#并排绘制图像和标签
plt.figure("image")
ax1 = plt.subplot(1,2,1)
ax2 = plt.subplot(1,2,2)
plt.sca(ax1)
plt.imshow(img)
plt.sca(ax2)
plt.imshow(label)
plt.show()
#图像截取
cropimg = img.crop((10, 10, 50, 100))
croplabel = label.crop((10, 10, 50, 100))
#图像随机截取
crop_size = (200, 100)
x=random.uniform(0, img.size[0]-crop_size[0])
y=random.uniform(0, img.size[1]-crop_size[1])
cropimg = img.crop((x, y, x+crop_size[0], y+crop_size[1]))
croplabel = label.crop((x, y, x+crop_size[0], y+crop_size[1]))
python库PIL、matplotlib的使用
最新推荐文章于 2023-11-10 16:14:49 发布