Created
January 3, 2016 08:56
-
-
Save kylemcdonald/2f1b9a255993bf9b2629 to your computer and use it in GitHub Desktop.
Minimal code for rendering a numpy array as an image in a Jupyter notebook in memory. Borrowed from the Deep Dream notebook.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())) |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's a lot simpler with matplotlib:
https://gist.github.com/prampey/dc0e3326131baaf35c380e7ddbf5119a