简体   繁体   中英

Does PIL work with skimage?

Why is is that when I do this:

from skimage import feature, io
from PIL import Image

edges = feature.canny(blimage)
io.imshow(edges)
io.show()

I get exactly what I want, that being the full edged-only image. But when I do this:

edges = feature.canny(blimage)
edges = Image.fromarray(edges)
edges.show()

I get a whole mess of random dots, lines and other things that have more resemble a Jackson Pollock painting than the image? Whats wrong, and how do I fix it such that I can get what I want through both methods?

for full code, visit my Github here:

https://github.com/Speedyflames/Image-Functions/blob/master/Image_Processing.py

Let's see what kind of image is produced by skimage.feature.canny :

edges = feature.canny(blimage)

>>> print(edges.dtype)
bool

It'a boolean image, True for white, False for black. PIL correctly identifies the datatype and tries to map it to its own 1-bit mode which is "1" (see PIL docs about it ). This looks broken though, it doesn't seem to properly get byte width or something like that.

There was an issue about it , they seem to have fixed the PIL to NumPy conversion but apparently the converse is still broken.

Anyway, long story short, your best bet to successfully convert a binary image from NumPy to PIL is converting it to grayscale:

edges_pil = Image.fromarray((edges * 255).astype(np.uint8))

>>> print edges_pil.mode
L

If you actually need a 1-bit image you can convert it afterwards with

edges_pil = edges_pil.convert('1')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM