Read an image with OpenCV and display it with Tkinter

后端 未结 3 922
生来不讨喜
生来不讨喜 2020-12-14 13:35

I have a very simple program on Ubuntu 14.04 LTS to read and display an image using OpenCV:

import cv2 #import OpenCV

img = cv2.imread(\'picture.jpg\') #rea         


        
3条回答
  •  南方客
    南方客 (楼主)
    2020-12-14 14:08

    You might want to take a look at this one. Here is something works for me:

    import numpy as np
    import cv2
    import Tkinter 
    import Image, ImageTk
    
    # Load an color image
    img = cv2.imread('img.png')
    
    #Rearrang the color channel
    b,g,r = cv2.split(img)
    img = cv2.merge((r,g,b))
    
    # A root window for displaying objects
    root = Tkinter.Tk()  
    
    # Convert the Image object into a TkPhoto object
    im = Image.fromarray(img)
    imgtk = ImageTk.PhotoImage(image=im) 
    
    # Put it in the display window
    Tkinter.Label(root, image=imgtk).pack() 
    
    root.mainloop() # Start the GUI
    

提交回复
热议问题