【问题标题】:How to save an image from a custom COCO dataset with its annotations overlaid on top of it如何从自定义 COCO 数据集中保存图像,其注释覆盖在图像之上
【发布时间】:2019-09-04 15:35:32
【问题描述】:

我创建了一个自定义 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)

但它不起作用。它只是保存原始图像,没有任何注释。

【问题讨论】:

标签: python json annotations scikit-image mscoco


【解决方案1】:

你可以保存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')

【讨论】:

    猜你喜欢
    • 2019-11-10
    • 2019-10-03
    • 1970-01-01
    • 1970-01-01
    • 2018-11-21
    • 1970-01-01
    • 1970-01-01
    • 2020-08-02
    • 2012-05-25
    相关资源
    最近更新 更多