6

I already have an in-memory file of an image. I would like to use skimage's io.imread (or equivalent) to read in the image. However, skimage.io.imread() take a file not a buffer

example buffer:

 <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 ... >

skimage.io.imread() just results in a numpy array.

benwiz
  • 2,167
  • 3
  • 22
  • 33

1 Answers1

3

Try converting the buffer into StringIO and then read it using skimage.io.imread()

import cStringIO

img_stringIO = cStringIO.StringIO(buf)
img = skimage.io.imread(img_stringIO)

You can also read it as :

from PIL import Image
img = Image.open(img_stringIO)
Abhijay Ghildyal
  • 4,044
  • 6
  • 33
  • 54
  • 1
    I tried these but both Image.open() and skimage.io.imread() are looking for filepaths – benwiz Aug 19 '16 at 19:53
  • 4
    [Image.open](https://pillow.readthedocs.io/en/3.4.x/reference/Image.html#PIL.Image.open) does support file objects. In python versions >= 3.0 you probably want to use `io.BytesIO`. Scikit image has several [plugins](http://scikit-image.org/docs/dev/api/skimage.io.html#find-available-plugins) that it can make use of. It is possible that not all of them support reading file objects. – SiggyF Dec 29 '16 at 14:56