Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

image does not display in ipython

Tags:

ipython

The image does not load if it is part of a while loop. For e.g. the following works as expected:

from IPython.display import Image
Image(filename='someimage.jpg')

But this does not work:

while True:
    Image(filename='someimage.jpg')
    break

update:

How do I display several images from a list?

like image 879
shantanuo Avatar asked Jan 30 '15 13:01

shantanuo


Video Answer


3 Answers

This works fine here:

from IPython.display import display, Image

path1 = "/some/path/to/image1.png"
path2 = "/some/path/to/image2.png"

for path in path1, path2:
    img = Image(path)
    display(img)
like image 145
cel Avatar answered Sep 29 '22 11:09

cel


Ok, I have exactly the same problem as apler details above. I am going thru a detailed example, where IPython is used to create an image. After several attempts, I have successfully displayed a .jpg image from a file, and confirmed Mac OSX Yosemite can use Python+PIL to display an image.

I had to uninstall PIL, and use Pillow instead, in order for the libjpeg library to be recognized correctly. The Python code creates an image - but it will not display the image.

I try to create the image with:

f = BytesIO()
PIL.Image.fromarray(a).save(f, 'jpeg')
clear_output(wait = True)
display(Image(data=f.getvalue()))

What I see when I run the little program in Python is:

<IPython.core.display.Image object> 
In : 

This In : looks to be an IPython prompt.

It appears that the code example I am working through is specifically designed to use IPython Notebook which is not the same as IPython, or interactive Python.

To use the code above, which is from the Google TensorFlow tutorial, one needs to configure an "IPython Notebook Server" and run something called "Jupyter". The documentation for Jupyter stuff is at: http://jupyter-notebook.readthedocs.io/en/latest/

The point apler makes is good. We should just be able to use plain Python to create a .jpg file, and display it on a Mac running OSX. I've pulled some code togther to show how to do this. It creates a .jpg image, writes it out to a file, and then displays it using Pillow version of PIL. (Note: I had to use pip to uninstall original PIL, then pip install Pillow, and I picked up the "libjpeg" library as a .dmg file from the Ethan.Tira-Thompson.com/Mac_OS_X_Ports.html site). You also need numpy and scipy.misc modules.

#
# --- Make+Show immediately a simulated Moire pattern as a .jpg file
#
# --- start Python and import this file with: import MakeShowMoire
# --- or use: execfile ("MakeShowMoire.py")
#
# --- import modules we need 
from PIL import Image, ImageDraw
import numpy as np
import scipy.misc
#
# --- Ok, lets go..
width = 1020
height = 710
channels = 3
#
img = np.zeros((height, width, channels), dtype=np.uint8)
xx, yy = np.mgrid[:height, :width]
circle = (xx - 100) ** 2 + (yy - 100) ** 2
for y in range(img.shape[0]):
    for x in range(img.shape[1]):
        r, g, b = circle[y][x], circle[y][x], circle[y][x]
        img[y] [x] [0] = r
        img[y] [x] [1] = g
        img[y] [x] [2] = b

#
# --- now, we have made the image, lets save it
scipy.misc.imsave("testimg.jpg", img)
#
# --- Since we have saved it, we should be able to display it
image = Image.open("testimg.jpg")
image.show()

Save this file as MakeShowMoire.py. To run this, start a terminal session, and run Python. I am using Python 2.7.10 [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang 600.0.39)] on darwin, and Mac OSX is 10.10.5 Yosemite. When Python runs, just enter:

>>> execfile ("MakeShowMoire.py")

And the example should display on your Mac, in a window called "tmpblahblah.BMP", which will be pitched when you close the window, but your testimg.jpg is retained, of course. I was using "import MakeShowMoire" at Python prompt to run the file, but the namespace does not map to the interactive Python session. If you use the execfile function, you can re-display the file contents with image.show() again. Hope this is useful.

like image 20
gemesyscanada Avatar answered Sep 29 '22 11:09

gemesyscanada


When working in IPython notebooks, the interpreter will give a response when typed as a separate command.

So for example:

>>>>a = 5
>>>>a
5

But if we were to do the same thing in a function or loop, we would not get the response.

So if you want to display images from within a loop/function, you will have to use the display function (IPython.display.display)

from IPython.display import display, Image

for image_path in images:  # images is a list of paths
    display(Image(image_path))

So the Image function is returning an image. When in the interpreter, it will get displayed, but otherwise it will not. To ensure that it is displayed, we use the display function.

This is similar to the print command. When in the interpreter, it is not necessary to use the print command to view variables, whereas while running a program it is.

like image 40
Samarth Hattangady Avatar answered Sep 29 '22 10:09

Samarth Hattangady