opencv python 按键_Python OpenCV:按下按键时刷新图像

这篇博客展示了如何在Python中使用OpenCV库通过按键事件来切换并显示不同的图像。用户可以按'w'键查看橙子的营养信息,'a'键查看苹果的营养信息,'s'键查看香蕉的营养信息,而空格键则结束程序。在每个按键事件之后,窗口都会被销毁并重新加载新的图像,确保每次显示的都是最新的内容。
摘要由CSDN通过智能技术生成

我已经解决了这个问题:

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()

我销毁窗口的每一个形象展示,这样一来,一致性,每一个按键敲击对应以新鲜的图片上传得到维护

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值