Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 917
Greystache Avatar asked May 05 '11 11:05

Greystache


2 Answers

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().

like image 53
Avaris Avatar answered Oct 17 '22 01:10

Avaris


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()
like image 34
Cédric Julien Avatar answered Oct 17 '22 01:10

Cédric Julien