我已经解决了这个问题:
import sys
import cv2
import os
def main():
File_Lst =[]
plat = sys.platform
#print plat
if plat == 'win32': #for windows operating system
File_dir = "C:\\Users\\user\\Desktop\\fruit\\"
elif plat == 'linux2': # for linux
File_dir = "/host/Users/user/Desktop/fruit/"
for file in os.listdir(File_dir):
File_Lst.append(file)
print File_Lst
welcome_index = File_Lst.index('welcome.jpg')
welcome_str = File_Lst[welcome_index]
orange_index = File_Lst.index('orange.jpg')
orange_str = File_Lst[orange_index]
apple_index = File_Lst.index('apple.jpg')
apple_str = File_Lst[apple_index]
banana_index = File_Lst.index('banana.jpg')
banana_str = File_Lst[banana_index]
doughnuts_index = File_Lst.index('doughnuts.jpg')
doughnuts_str = File_Lst[doughnuts_index]
img = cv2.imread(File_dir + welcome_str)
cv2.destroyAllWindows()
cv2.imshow("Press KEYS to know which food is good or bad", img)
while True:
k = cv2.waitKey(0)
if k == ord('w'): # wait for 'w' key to upload orange nutrition information
img = cv2.imread(File_dir + orange_str)
newx,newy = img.shape[1]/2,img.shape[0]/2 #new size (w,h)
img = cv2.resize(img,(newx,newy))
cv2.destroyAllWindows()
cv2.imshow("Orange Nutritional Information", img)
elif k == ord('a'): # wait for 'w' key to upload apple nutrition information
img = cv2.imread(File_dir + apple_str)
newx,newy = img.shape[1]/2,img.shape[0]/2 #new size (w,h)
img = cv2.resize(img,(newx,newy))
cv2.destroyAllWindows()
cv2.imshow("Apple Nutritional Information", img)
elif k == ord('s'): # wait for 'w' key to upload apple nutrition information
img = cv2.imread(File_dir + banana_str)
newx,newy = img.shape[1]/2,img.shape[0]/2 #new size (w,h)
img = cv2.resize(img,(newx,newy))
cv2.destroyAllWindows()
cv2.imshow("Banana Nutritional Information", img)
elif k == 32:
break
cv2.destroyAllWindows()
else:
img = cv2.imread(File_dir + doughnuts_str)
cv2.destroyAllWindows()
cv2.imshow("Bad, Have good eating habits CHUMP", img)
continue
main()
我销毁窗口的每一个形象展示,这样一来,一致性,每一个按键敲击对应以新鲜的图片上传得到维护