Python-OpenCV-在tkinter的canvas控件中显示OpenCV图像

tkinter是python中界面设计常用的库,但是对图片显示的仅支持GIF 和 PGM/PPM 文件格式。和OpenCV没有直接的转换函数,这里另外引进了python常用的图像库PIL来间接的进行转换。
实现了在在tkinter的canvas上显示OpenCV图像。效果如图:

Python-OpenCV-在tkinter的canvas控件中显示OpenCV图像_第1张图片
实现代码如下:

# -*- coding: utf-8 -*-
"""
Created on Fri Mar 13 14:37:33 2020

@author: xin
"""
from tkinter import *
import cv2 as cv
from a import Image,ImageTk

def Showimage(imgCV_in,canva,layout="null"):
    """
    Showimage()是一个用于在tkinter的canvas控件中显示OpenCV图像的函数。
    使用前需要先导入库
    import cv2 as cv
    from PIL import Image,ImageTktkinter
    并注意由于响应函数的需要,本函数定义了一个全局变量 imgTK,请不要在其他地方使用这个变量名!
    参数:
    imgCV_in:待显示的OpenCV图像变量
    canva:用于显示的tkinter canvas画布变量
    layout:显示的格式。可选项为:
        "fill":图像自动适应画布大小,并完全填充,可能会造成画面拉伸
        "fit":根据画布大小,在不拉伸图像的情况下最大程度显示图像,可能会造成边缘空白
        给定其他参数或者不给参数将按原图像大小显示,可能会显示不全或者留空
    """
    global imgTK
    canvawidth = int(canva.winfo_reqwidth())
    canvaheight = int(canva.winfo_reqheight())
    sp = imgCV_in.shape
    cvheight = sp[0]#height(rows) of image
    cvwidth = sp[1]#width(colums) of image
    if (layout == "fill"):
        imgCV = cv.resize(imgCV_in,(canvawidth,canvaheight), interpolation=cv.INTER_AREA)
    elif(layout == "fit"):
        if (float(cvwidth/cvheight) > float(canvawidth/canvaheight)):
            imgCV = cv.resize(imgCV_in,(canvawidth,int(canvawidth*cvheight/cvwidth)), interpolation=cv.INTER_AREA)
        else:
            imgCV = cv.resize(imgCV_in,(int(canvaheight*cvwidth/cvheight),canvaheight), interpolation=cv.INTER_AREA)
    else:
        imgCV = imgCV_in
    imgCV2 = cv.cvtColor(imgCV, cv.COLOR_BGR2RGBA)#转换颜色从BGR到RGBA
    current_image = Image.fromarray(imgCV2)#将图像转换成Image对象
    imgTK = ImageTk.PhotoImage(image=current_image)#将image对象转换为imageTK对象
    canva.create_image(0,0,anchor = NW, image = imgTK)

root = Tk()
root.title("OpenCV Win")
canva = Canvas(root, width=800, height=600,bg="gray")
canva.pack()
img = cv.imread("test.jpg")
Showimage(img,canva,"fill")
root.mainloop()

版权声明:本文为CSDN博主「※陈默※」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_38179212/article/details/104968754

由于需求不同,进行自我改进:

@staticmethod
def showimage(imgCV_in, canva, layout="null"):
    """
    from PIL import Image,ImageTktkinter
    并注意由于响应函数的需要,本函数定义了一个全局变量 imgTK,请不要在其他地方使用这个变量名!
    参数:
    imgCV_in:待显示的OpenCV图像变量
    canva:用于显示的tkinter canvas画布变量
    layout:显示的格式。可选项为:
        "fill":图像自动适应画布大小,并完全填充,可能会造成画面拉伸
        "fit":根据画布大小,在不拉伸图像的情况下最大程度显示图像,可能会造成边缘空白
        给定其他参数或者不给参数将按原图像大小显示,可能会显示不全或者留空
    """
    global imgTK
    try:
        canvawidth = int(canva.winfo_reqwidth())
        canvaheight = int(canva.winfo_reqheight())
        sp = imgCV_in.shape
        cvheight = sp[0]  # height(rows) of image
        cvwidth = sp[1]  # width(colums) of image
        if layout == "fill":
            imgCV = cv2.resize(imgCV_in, (canvawidth, canvaheight), interpolation=cv2.INTER_AREA)
        elif layout == "fit":
            if (float(cvwidth / cvheight) > float(canvawidth / canvaheight)):
                imgCV = cv2.resize(imgCV_in, (canvawidth, int(canvawidth * cvheight / cvwidth)),
                                   interpolation=cv2.INTER_AREA)

            else:
                imgCV = cv2.resize(imgCV_in, (int(canvaheight * cvwidth / cvheight), canvaheight),
                                   interpolation=cv2.INTER_AREA)
        else:
            if cvwidth>800 or cvheight>480:
                imgCV = cv2.resize(imgCV_in, (canvawidth, canvaheight), interpolation=cv2.INTER_AREA)
            else:
                imgCV = imgCV_in
        imgCV2 = cv2.cvtColor(imgCV, cv2.COLOR_BGR2RGBA)  # 转换颜色从BGR到RGBA
        current_image = Image.fromarray(np.uint8(imgCV2))  # 将图像转换成Image对象
        imgTK = ImageTk.PhotoImage(image=current_image)  # 将image对象转换为imageTK对象
        canva.create_image(0, 0, anchor=NW, image=imgTK)
    except:
        showwarning("警告","未导入图片!!!")

你可能感兴趣的:(python,opencv,opencv,python,canvas)