-
-
Save kylemcdonald/2f1b9a255993bf9b2629 to your computer and use it in GitHub Desktop.
import PIL.Image | |
from cStringIO import StringIO | |
import IPython.display | |
import numpy as np | |
def showarray(a, fmt='png'): | |
a = np.uint8(a) | |
f = StringIO() | |
PIL.Image.fromarray(a).save(f, fmt) | |
IPython.display.display(IPython.display.Image(data=f.getvalue())) |
Actually,
PIL.Image.fromarray(a)
in jupyter notebook, is enough.
Thanks
things can be made easier if you have cv2
: https://gist.github.com/ctmakro/3ae3cd9538390b706820cd01dac6861f
@ctmakro - thank you for the elegant solution -- image shows directly in Jupyter Lab 👍
This works for Google colab (mostly for my own reference)
import IPython.display
import PIL.Image
IPython.display.display(PIL.Image.fromarray(a))
It's a lot simpler with matplotlib:
from matplotlib.pyplot import imshow
%matplotlib inline
#image is a numpy array
imshow(image)
https://gist.github.com/prampey/dc0e3326131baaf35c380e7ddbf5119a
imshow does not faithfully render an array as an image and is generally annoying. There is good reason to use PIL for this
That’s strange, Imshow
Has worked for me with any 3D NumPy array. Do you have an example? @rueberger
In fact, it also works with PIL images out-of-the-box.
import PIL.Image
imshow(PIL.Image.open(img_path))
It interpolates by default. From the docs "The number of pixels used to render an image is set by the Axes size and the dpi of the figure. This can lead to aliasing artifacts when the image is resampled because the displayed image size will usually not match the size of X".
Just a lot of nonsense to wade through when you just want to see a faithful representation of an image array
IPython can display PIL.Image directly! So you can skip the encode-to-png and decode-from-png:
IPython.display.display(PIL.Image.fromarray(a))