scikit-image save image to a bytestring

懵懂的女人 提交于 2019-12-12 17:18:36

问题


I'm using scikit-image to read an image:

img = skimage.io.imread(filename)

After doing some manipulations to img, I'd like to save it to an in-memory file (a la StringIO) to pass off to another function, but it looks like skimage.io.imsave requires a filename, not a file handle.

I'd like to avoid hitting the disk (imsave followed by read from another imaging library) if at all possible. Is there a nice way to get imsave (or some other scikit-image-friendly function) to work with StringIO?


回答1:


scikit-image stores images as numpy arrays, therefore you can use a package such as matplotlib to do so:

import matplotlib.pyplot as plt
from StringIO import StringIO

s = StringIO()

plt.imsave(s, img)

This may be worth adding as default behaviour to skimage.io.imsave, so if you want you can also file an issue at https://github.com/scikit-image/scikit-image.



来源:https://stackoverflow.com/questions/16164283/scikit-image-save-image-to-a-bytestring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!