scikit-image将图像保存到字节串 [英] scikit-image save image to a bytestring
问题描述
我正在使用 scikit-image 来读取图像:
I'm using scikit-image to read an image:
img = skimage.io.imread(filename)
对img
做一些操作后,我想将其保存到内存文件中(例如 skimage.io.imsave
需要文件名,而不是文件句柄.
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.
如果可能的话,我想避免打磁盘(imsave
然后从另一个映像库中读取).有什么好方法可以使imsave
(或其他一些对scikit-image友好的功能)与StringIO
一起使用?
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
?
推荐答案
更新:2020-05-07
我们现在建议使用imageio
库进行图像读取和写入.另外,在Python 3中,StringIO更改为BytesIO:
We now recommend using the imageio
library for image reading and writing. Also, with Python 3, StringIO changes to BytesIO:
from io import BytesIO
import imageio
buf = BytesIO()
imageio.imwrite(buf, image, format='png')
scikit-image
将图像存储为numpy数组,因此可以使用诸如matplotlib
的程序包来实现:
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)
这可能值得添加为skimage.io.imsave
的默认行为,因此,如果您愿意,也可以在 https://github.com/scikit-image/scikit-image .
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.
这篇关于scikit-image将图像保存到字节串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!