使用 scikit-image 读取图像缓冲区

ben*_*wiz 6 python io buffer numpy scikit-image

我已经有一个图像的内存文件。我想使用 skimage 的 io.imread (或等效的)来读取图像。但是,skimage.io.imread() 采用文件而不是缓冲区

示例缓冲区:

 <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 00 00 01 00 01 00 00 ff db 00 43 00 03 02 02 02 02 02 03 02 02 02 03 03 03 03 04 06 04 04 04 04 04 08 06 06 05 ... >
Run Code Online (Sandbox Code Playgroud)

skimage.io.imread() 只是产生一个 numpy 数组。

Abh*_*yal 3

尝试将缓冲区转换为 StringIO,然后使用读取它skimage.io.imread()

import cStringIO

img_stringIO = cStringIO.StringIO(buf)
img = skimage.io.imread(img_stringIO)
Run Code Online (Sandbox Code Playgroud)

您也可以将其读作:

from PIL import Image
img = Image.open(img_stringIO)
Run Code Online (Sandbox Code Playgroud)

  • [Image.open](https://pillow.readthedocs.io/en/3.4.x/reference/Image.html#PIL.Image.open) 确实支持文件对象。在 python 版本 &gt;= 3.0 中,您可能想使用 `io.BytesIO`。Scikit 图像有几个可以使用的插件。可能并非所有这些都支持读取文件对象。 (4认同)