[英]resizing an image in tkinter
the following code plays a given video file on a tkinter window:以下代码在 tkinter window 上播放给定的视频文件:
from tkinter import *
from PIL import ImageTk, Image
import cv2
root = Tk()
main_label = Label(root)
main_label.grid()
# Capture from camera
cap = cv2.VideoCapture("video.mp4")
# function for video streaming
def video_stream():
ret, frame = cap.read()
cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
img = Image.fromarray(cv2image)
tk_img = ImageTk.PhotoImage(image=img)
main_label.configure(image=tk_img)
main_label.tk_img = tk_img
main_label.after(20, video_stream)
video_stream()
root.mainloop()
my question is how can I resize the video to be played in a 500 by 500 resolution for example?我的问题是如何调整以 500 x 500 分辨率播放的视频大小?
First I would like to mention a possible problem.首先我想提一个可能的问题。
Always check whether the frame
returns or not.始终检查
frame
是否返回。 Otherwise your application will crash.否则你的应用程序会崩溃。
ret, frame = cap.read()
if ret:
cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
Now resize
your frame现在
resize
你的框架
# function for video streaming
ret, frame = cap.read()
if ret:
cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
cv2image = cv2.resize(cv2image, (500, 500))
Correct Code:正确代码:
from tkinter import *
from PIL import ImageTk, Image
import cv2
root = Tk()
main_label = Label(root)
main_label.grid()
# Capture from camera
cap = cv2.VideoCapture("video.mp4")
# function for video streaming
def video_stream():
ret, frame = cap.read()
if ret:
cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
cv2image = cv2.resize(cv2image, (500, 500))
img = Image.fromarray(cv2image)
tk_img = ImageTk.PhotoImage(image=img)
main_label.configure(image=tk_img)
main_label.tk_img = tk_img
main_label.after(20, video_stream)
video_stream()
root.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.