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

I am writing some kind of iterative image processing algorithm in a script (I do not want to use iPython) and I would like to render the image that I generate after each iteration. This is very easy to do in Matlab without blocking the main thread, but I'm trying my best to do it in Python.

In pylab, the show () function is blocked, and I need to close the window to continue executing my script. I saw that some people use the ion () function, but this has no effect in my case, for example:

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

still blocked. I also saw people saying that โ€œusing a draw instead of a plotโ€ can solve this problem. However, I do not use the plot, but imshow / show, is there something I am missing here?

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

I am using Ubuntu 10.10.

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

+6
source share
2 answers

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

pylab.show() will start Tk mainloop, therefore, it blocks. Whereas pylab.draw() will force you to draw a shape at this point. Since you are using pylab.ion() , numbers are already created. But at the end of the script you need to put pylab.show() , otherwise the numbers will be closed when the script ends, since there is no mainloop. One side effect is that you cannot interact with numbers until you reach pylab.show() .

+3
source

you can try filling 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() 
+2
source

Source: https://habr.com/ru/post/887358/


All Articles