Обрезку можно легко выполнить, просто вырезав нужную часть из массива.Например, image[100:200, 50:100, :]
разрезает часть между пикселями 100 и 200 в направлении y (по вертикали) и часть между пикселями 50 и 100 в направлении x (по горизонтали).
См. Этот рабочий пример:
import matplotlib.pyplot as plt
mydic = {
"annotations": [
{
"class": "rect",
"height": 98,
"width": 113,
"x": 177,
"y": 12
},
{
"class": "rect",
"height": 80,
"width": 87,
"x": 373,
"y": 43
}
],
"class": "image",
"filename": "https://i.stack.imgur.com/9qe6z.png"
}
def crop(dic, i):
image = plt.imread(dic["filename"])
x0 = dic["annotations"][i]["x"]
y0 = dic["annotations"][i]["y"]
width = dic["annotations"][i]["width"]
height = dic["annotations"][i]["height"]
return image[y0:y0+height , x0:x0+width, :]
fig = plt.figure()
ax = fig.add_subplot(121)
ax.imshow(plt.imread(mydic["filename"]))
ax1 = fig.add_subplot(222)
ax1.imshow(crop(mydic, 0))
ax2 = fig.add_subplot(224)
ax2.imshow(crop(mydic, 1))
plt.show()