用 OpenCV 读取图像并用 Tkinter 显示 [英] Read an image with OpenCV and display it with Tkinter
问题描述
我在 Ubuntu 14.04 LTS 上有一个非常简单的程序来使用 OpenCV 读取和显示图像:
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') #read a picture using OpenCV
cv2.imshow('image',img) # Display the picture
cv2.waitKey(0) # wait for closing
cv2.destroyAllWindows() # Ok, destroy the window
我的问题:
如何在 OpenCV 中继续读取图片但使用 Tkinter 显示它?
How can I keep reading the picture in OpenCV but display it using Tkinter ?
我问这个是因为我想为我的程序制作一个接口,但 OpenCV 无法做到这一点,所以我需要 Tkinter.但是,我必须使用 OpenCV 在后台进行所有图像处理.仅显示结果必须使用 Tkinter 完成.
I ask this because I want to make an interface for my program but OpenCV is not able to do it so I need Tkinter for this. However, all the image processing I must do it on the background using OpenCV. Only displaying the results must be done using Tkinter.
从上面的答案,我换了一行:
From the answer above, I change the line:
im = Image.open('slice001.hrs').convert2byte()
致:
im=cv2.imread() # (I imported cv2)
但是我遇到了一个错误.
But I got an error.
如果有任何提示,我将不胜感激.
I would appreciate any hints.
推荐答案
你可能想看看 这个.这是对我有用的东西:
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
这篇关于用 OpenCV 读取图像并用 Tkinter 显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!