Does PIL work with skimage?


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





Check what kind of type skimage returns and select the corresponding mode in your argument to fromarray to make sure, there is no type-based error (which is likely).
– sascha
Jun 29 at 17:09




1 Answer
1



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


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.


True


False


"1"



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


PIL


NumPy



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


NumPy


PIL


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






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Render GeoTiff to on browser with leaflet

How to get chrome logged in user's email id through website

using states in a react-navigation without redux