对不起,我的题目不太容易通读。在
这是我的问题。在
我正在尝试拍摄一个图片文件并循环遍历每个像素。在
我想在每个像素上显示像素文件图像的matplotlib图形(完成)
一旦图形显示出来,我需要我的循环暂停,而用户可以按’b’或’c’来分类像素的颜色,然后用空格键关闭图形。在
一旦这个操作完成,我希望它加载下一个像素。但是我遇到的问题是for循环没有暂停。如果我在for循环中放一个while循环,这个图形就永远不会完成加载(当鼠标滚动到它上面时,它会变成一个白色屏幕,鼠标会转向加载圆圈)。在
我对事件处理不熟悉,所以我可能错过了一些方法。在
我也无法使用matplotlibs交互模式进行任何操作。在
下面是我的代码,这是非常粗糙的,我只是从今天早上开始工作。类中唯一使用的方法是init()。在from scipy.misc import imread
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
class images(object):
def init(self,img,flat=False,mod=’RGB’,show=True):
‘File types to compare to’
file_type_list =[‘.jpg’,’.bmp’,’.gif’,’.png’]
‘Create assertion error if not right fiel type’
assert(any(img[-4:] for x in file_type_list)),”Wrong file formate please use: JPEG, BMP, GIF, or PNG”
‘Read image file to numpy array’
self.img=imread(img,flatten=flat,mode=mod)
‘Show image in matplotlib plot if selected by user’
if show == True:
fig, ax = plt.subplots()
ax.imshow(self.img)
def arr_to_vector(self):
‘Convert 3D image array into 2D vector [no. pixels,3(RGB)]’
self.img_vector = self.img.reshape(-1,self.img.shape[2])
return self.img_vector
def gray_vector(self):
‘Convert 3D image array into 1D vector [no. pixels in grayscale]’
img_vector = self.img.reshape(-1,self.img.shape[2])
self.g_img_vector = np.mean(img_vector,axis=1)
return self.g_img_vector
def plotter(self):
self.fig = plt.figure()
plt.imshow(self.img)
def disp_pixel(i):
tmp = allsamecolor(Img.img,i)
fig = plt.figure()
plt.imshow(tmp)
plt.show()
cid = fig.canvas.mpl_connect(‘key_press_event’,on_key)
return fig.number, fig
def on_key(event):
if event.key == ‘ ‘:
plt.close(event.canvas.figure)
return event.key
else:
if event.key == ‘b’:
print(event.key)
labels.append(‘black’)
elif event.key == ‘c’:
print(event.key)
labels.append(‘copper’)
def assign(x,i):
x = i
return x
def allsamecolor(img, i):
fv = np.vectorize(assign)
ret = fv(img,i)
return ret
img = ‘C:/Temp/test.jpg’
Img = images(img,show=False)
v = Img.arr_to_vector()
labels = []
n = 0
for i in v:
x, fig = disp_pixel(i)
print(x)
input(“Press to continue”)
Original: https://blog.csdn.net/weixin_30636449/article/details/113981825
Author: xingming Zhenshi
Title: python怎样循环等待输入_如何暂停for循环并等待用户输入matplotlib
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/770139/
转载文章受原作者版权保护。转载请注明原作者出处!