Greystache
Greystache

Reputation: 268

How to display an image with Pylab from a script in a non blocking way

I am writing some iterative image processing algorithm in a script (I don't want to be using iPython), and I would like to visualize the image I generate after each iteration. That's very easy to do in Matlab, without blocking the main thread, but I am struggling to do it in Python.

In pylab the show() function is blocking and I need to close the window to continue the execution of my script. I have seen that some people use the ion() function, but it has no effect in my case, for example:

pylab.ion()
img = pylab.imread('image.png')
pylab.imshow(img)
pylab.show()

is still blocking. I also saw people saying that "using draw instead of plot" can solve this. However, I am not using plot but imshow/show, is there something that I am missing here?

On the other hand, the PIL also has some display functions, but it seems to generate a temporary image and then display it with imagemagick, so I assume there is no way here to display an image and update it in the same window with this method.

I am using Ubuntu 10.10.

Does anyone know how to do it simply, or do I have to start using something like Qt to have a minimal GUI that I can update easily?

Upvotes: 6

Views: 9424

Answers (2)

Avaris
Avaris

Reputation: 36715

Try using pylab.draw() instead of pylab.show().

pylab.show() will start a Tk mainloop, hence it is blocking. Whereas pylab.draw() will force a draw of figure at that point. Since you are using pylab.ion(), figures are created already. But at the end of the script you have to put a pylab.show() otherwise figures will be closed when script finishes as there is no mainloop. One side effect is that, you can't interact with the figures until you reach pylab.show().

Upvotes: 3

Cédric Julien
Cédric Julien

Reputation: 80761

you can try to thread your pylab stuff :

import pylab
import threading

pylab.ion()
img = pylab.imread('map.png')

def create_show():
    pylab.imshow(img)
    pylab.show()

thread = threading.Thread(target=create_show)
thread.start()

#do your stuff

thread.join()

Upvotes: 2

Related Questions