我正在使用 scikit-image 读取图像:
img = skimage.io.imread(filename)
在对 img 进行一些操作后,我想将它保存到内存中的文件(a la StringIO )以传递给另一个函数,但看起来 skimage.io.imsave 需要一个文件名,不是文件句柄。
如果可能的话,我想避免访问磁盘(imsave 然后从另一个图像库读取)。有什么好的方法可以让 imsave(或其他一些 scikit-image-friendly 函数)与 StringIO 一起工作?
请您参考如下方法:
更新:2020-05-07
我们现在建议使用imageio 库进行图像读写。此外,在 Python 3 中,StringIO 更改为 BytesIO:
from io import BytesIO
import imageio
buf = BytesIO()
imageio.imwrite(buf, image, format='png')
scikit-image 将图像存储为 numpy 数组,因此您可以使用 matplotlib 等包来执行此操作:
import matplotlib.pyplot as plt
from StringIO import StringIO
s = StringIO()
plt.imsave(s, img)
这可能值得添加为 skimage.io.imsave 的默认行为,因此如果您愿意,也可以在 https://github.com/scikit-image/scikit-image 提交问题。 .
