我创建了一个自定义COCO数据集。现在假设我在image_data中有有效的图像元数据。我可以用
import skimage.io as io
import matplotlib.pyplot as plt
image_directory ='my_images/'
image = io.imread(image_directory + image_data['file_name'])
plt.imshow(image); plt.axis('off')
pylab.rcParams['figure.figsize'] = (8.0, 10.0)
annotation_ids = example_coco.getAnnIds(imgIds=image_data['id'], catIds=category_ids, iscrowd=None)
annotations = example_coco.loadAnns(annotation_ids)
example_coco.showAnns(annotations)此时,我将能够看到覆盖在图像上的注释。但是,我想保存图像,并在其上加上注释。我怎么能这么做?我试过了
io.imsave(fname="test.png", arr=image)但不起作用。它只是保存原始图像,没有任何注释。
发布于 2019-09-04 16:15:00
可以使用以下方法保存figure/plot
plt.savefig("test.png", bbox_inches='tight', pad_inches=0)有一些参数可以删除边距。
工作实例
import skimage.io as io
import matplotlib.pyplot as plt
image = io.imread("https://homepages.cae.wisc.edu/~ece533/images/lena.png")
plt.imshow(image)
plt.axis('off')
plt.annotate("Lena", (10, 20))
plt.savefig("test.png", bbox_inches='tight', pad_inches=0) # if you want to display then `savefig()` has to be before `show()`
#plt.show()

最终,您可以使用PIL/pillow直接在图像上进行绘图。
import skimage.io as io
from PIL import Image, ImageDraw, ImageFont
image = io.imread("https://homepages.cae.wisc.edu/~ece533/images/lena.png")
img = Image.fromarray(image)
draw = ImageDraw.Draw(img)
draw.text((10,10), "Lena", font=ImageFont.truetype("arial", 20), fill=(0,0,0))
img.save('test.png')https://stackoverflow.com/questions/57791698
复制相似问题