简体   繁体   中英

Python - matplotlib.pyplot.imshow won't work in a if//while

I'm trying to display an image (a N*P numpy array) in a while loop with imshow but it doesn't seem to work.

When I just use "plt.imshow(image,cmap='gray')" with no indent it works perfectly.

However, when I try to use it from a while loop like

continue=True
while continue:
    plt.imshow(image,cmap='gray')
    continue=input()

or

i=0
while i<10:
    plt.imshow(image,cmap='gray')
    i+=1

the pyplot window just freeze and python crash (same problem with a if).

I don't understand why it does this, does anybody here knows? Thanks alot in advance :)

EDIT : I try to use it like this in my program

import matplotlib.pyplot as plt
import numpy as np
import PIL.Image as im

image=np.array(im.open("Blabla.jpg").convert('L')).astype(np.float32)

plt.imshow(image,cmap='gray') #This works -> ONLY when there is not a while after

keepGoin = True #But this doesn't
while keepGoin:
    plt.imshow(image,cmap='gray')
    keepGoin=input()

EDIT 2 :

I made a mistake : it seems that imshow pauses when a while//if is used, even if the while loop is right after the imshow. And when the while loop ends, an image is finally displayed. Maybe using this kind of loop simply pauses the process ?

So I tried to use a secondary function aiming to display the image "out" of the while but it doesn't work either, the result is the same : the pyplot window freeze during the while loop, and the image is displayed right after the loop ends.

EDIT 3 :

So after some more tries, I am pretty sure the problem is not calling imshow from a while but the fact that the loop pauses the execution of pyplot, even if it is called BEFORE the loop (If I call imshow and then use a loop, the pyplot window freeze until the loop ends)

According to some new research I've made, plt.pause could help me but I've an error when I try this.

Here are some observations which might help you debug. The code and statements below only apply to Python 2.x .

Using the code below, you would have to type the word True or False on the console from which you started the program, for it to not crash after the first showing, because input() expects some sort of input and throws EOF if you do not give it anything.

import  matplotlib.pyplot as plt
import numpy as np
import PIL.Image as im

image=np.array(im.open("Blabla.jpg").convert('L')).astype(np.float32)

plt.ion()
plt.imshow(image,cmap='gray')

keepGoin = True #But this doesn't
while keepGoin:
    plt.imshow(image,cmap='gray')
    keepGoin=input()

If you change it to raw_input , it is more forgiving in terms of not crashing with EOF but will still terminate unless you give it some input. However, changing the loop to be independent of the input paused it for me until I pushed Enter on the terminal.

while True:
    plt.imshow(image,cmap='gray')
    raw_input()

If you are using a Spider the easier way is configure to show the image in console because in the console the image not freeze. To configure the spider to show the image on console: preferences -> python console -> graphics -> output graphic -> here: choice inline instead of automatic

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM