python tkinter 显示摄像头

def threadCamera(threadName,udpClient):
    cap = cv2.VideoCapture(0)
    while(True):
        #======  图片获取 ======
        #读取原始图
        ret,frame=cap.read()
        #获取灰度图
        # gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)

        #======  图片保存至本地 ======
        #两种图片的保存类型 jpg png
        cv2.imwrite('imgTest1.jpg', frame, [cv2.IMWRITE_JPEG_QUALITY, 50])
        # cv2.imwrite('imgTest1.png', frame, [cv2.IMWRITE_PNG_COMPRESSION, 50])

        #======  转换成base64  =====
        base64_data=None
        with open("imgTest1.jpg","rb") as f: 
            base64_data = base64.b64encode(f.read()) 
            pass

        #======  UDP 发送  =====

        if base64_data != None:
            sendBytes = getPicMsgHead() + base64_data
            sendSize=len(sendBytes)
            index=0
            while sendSize > 0 :
                if sendSize < BUFSIZE :
                    udpClient.sendBytes(DEFAULT_IP,int(DEFAULT_PORT),sendBytes[index:index+sendSize])
                else :
                    udpClient.sendBytes(DEFAULT_IP,int(DEFAULT_PORT),sendBytes[index:index+ BUFSIZE])
                sendSize = sendSize - BUFSIZE 
                index = index + BUFSIZE 

        #======  Image对象的两种来源,本地及 opencv mat ======
        # 从本地文件获取image对象
        # myimg = ImageTk.PhotoImage(file='imgTest1.jpg')
        # 从opencv的mat 获取Image对象
        #<class 'numpy.ndarray'> 
        imgData = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
        #<class 'PIL.Image.Image'>
        imgData2 = Image.fromarray(imgData)
        #缩放
        imgResized = imgData2.resize((CAMERA_IMG_WIDTH, CAMERA_IMG_HEIGHT), Image.ANTIALIAS)
        #<class 'PIL.ImageTk.PhotoImage'>
        img = ImageTk.PhotoImage(imgResized)        
        

        #======  Label 显示图片 ======
        #显示 第一句设置,第二局保持,否则闪烁
        mainFrame.lbl_cam.config(image=img, anchor='nw')
        mainFrame.lbl_cam.image = img
        # mainFrame.lbl_pic.config(image=img, anchor='nw')
        # mainFrame.lbl_pic.image = img
    pass

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

成震1971

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值