3

I have 2 modules in my project: first works with image in bytes format, second requires skimage object. I need to combine them.

I have this code:

import io
from PIL import Image
import skimage.io

area = (...)
image = Image.open(io.BytesIO(image_bytes))
image = Image.crop(area)
image = skimage.io.imread(image)

But i get this error: enter image description here

How can i convert an image (object/variable) to skimage? I don't necessarily need PIL Image, this is just one way to work with bytes image, cause i need to crop my image

Thanks!

Neighbourhood
  • 166
  • 3
  • 13

1 Answers1

10

Scikit-image works with images stored as Numpy arrays - same as OpenCV and wand. So, if you have a PIL Image, you can make a Numpy array for scikit-image like this:

# Make Numpy array for scikit-image from "PIL Image"
na = np.array(YourPILImage)

Just in case you want to go the other way, and make a PIL Image from a Numpy array, you can do:

# Make "PIL Image" from Numpy array
pi = Image.fromarray(na)
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • But how skimage can load this array? I mean, skimage.io.imread() takes str type - image file path @mark-setchell – Neighbourhood Nov 30 '20 at 14:35
  • If you have a file on disk called `"image.jpg"`, you need `image = skimage.io.imread("image.jpg")` – Mark Setchell Nov 30 '20 at 14:52
  • no, i dont have it. i already have an image object i work with, and it can't be saved as file on server to load it for skimage. That is why i'm asking. It just an object image variable during the program @Mark-Setchell – Neighbourhood Nov 30 '20 at 14:54
  • I don't understand what you have. Let's suppose you have something called `"thing"` so please show the output from `print(type(thing))` and please show the first few bytes of `thing`. Thank you. – Mark Setchell Nov 30 '20 at 14:57
  • if print(image_bytes) then b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff\xdb\x00C\x00\x08\x06\x06\x07\x06\x05\x08\x07\x07\x07 etc – Neighbourhood Nov 30 '20 at 15:01
  • sorry, maybe my english is too bad to explain what i need :( – Neighbourhood Nov 30 '20 at 15:03
  • 1
    @Neighbourhood: I fail to see what the problem is. Did you try Mark’s code? Where you have `image = skimage.io.imread(image)` instead use `image = np.array(image)`. – Cris Luengo Nov 30 '20 at 15:27