Sorry my question title is not very easy to read through.
This is my problem.
I am trying to take a picture file and loop through every pixel.
On each pixel I want to display a matplotlib figure of the image of the pixel file (done.)
Once the figure is displayed I need my loop to pause while the user classifies the color of the pixel by either pressing 'b' or 'c' and then closes the figure with the space bar.
Once this operation is done I want it to load the next pixel. However the issue I am having is the for loop doesn't pause. If I put a while loop inside the for loop the figure doesn't ever finish loading (stays as a white screen and the mouse turns to the loading circle when scrolling over it).
I am new to event handling so maybe I am missing some way to do this there.
I also cannot get anything with matplotlibs interactive mode to work.
Below is my code, it is very rough I have only been working on it since this morning. The only method in the class being used is 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 <ENTER> to continue")