5

我正在使用scikit-image读取图像:

img = skimage.io.imread(filename)

在对 进行一些操作后img,我想将其保存到内存文件(a la StringIO)以传递给另一个函数,但它看起来skimage.io.imsave需要文件名,而不是文件句柄。

如果可能的话,我想避免碰到磁盘(imsave然后从另一个映像库中读取)。有没有一种很好的方法来使用imsave(或其他一些对 scikit-image 友好的功能)StringIO

4

1 回答 1

6

更新: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提交问题。

于 2013-04-23T08:59:08.837 回答