【问题标题】:scikit-image save image to a bytestringscikit-image 将图像保存到字节串
【发布时间】:2013-04-16 08:50:51
【问题描述】:

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

img = skimage.io.imread(filename)

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

如果可能的话,我想避免碰到磁盘(imsave 然后从另一个映像库中读取)。有没有一种好方法可以让 imsave(或其他一些对 scikit-image 友好的函数)与 StringIO 一起工作?

【问题讨论】:

    标签: python in-memory stringio scikit-image


    【解决方案1】:

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

    【讨论】:

    猜你喜欢
    • 2016-07-12
    • 2016-11-23
    • 1970-01-01
    • 1970-01-01
    • 2014-08-06
    • 1970-01-01
    • 1970-01-01
    • 2018-01-19
    • 2022-01-14
    相关资源
    最近更新 更多