Search code examples
pythonimage-processingcomputer-visionscikit-imageopencv

Converting PIL.Image to skimage


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!


Solution

  • 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)