PaulleDemon / tkVideoPlayer

Video player for tkinter.
MIT License
79 stars 25 forks source link

Drawing over the Videoplayer #30

Closed zaynabtariq closed 11 months ago

zaynabtariq commented 1 year ago

Hi @Meghansh-Maurya, You cannot draw over window objects in Tkinter canvas. You can clone this project, then instead of inheriting from Label, inherit from Canvas, then use canvas.create_image(x, y, anchor=NW, image=image) to draw the video into the canvas; you may then be able to overlay other drawings.

Originally posted by @PaulleDemon in https://github.com/PaulleDemon/tkVideoPlayer/issues/25#issuecomment-1457479719

Hi @PaulleDemon, I tried doing this but I keep getting an error. How do make this work? Exception in thread Thread-1: Traceback (most recent call last): File "threading.py", line 973, in _bootstrap_inner self.run() File "threading.py", line 910, in run self._target(*self._args, **self._kwargs) File "tkvideoplayer.py", line 131, in _load self._set_frame_size() File "tkvideoplayer.py", line 99, in _set_frame_size self.config(width=150, height=100, image=self.current_imgtk) File "init.py", line 1646, in configure return self._configure('configure', cnf, kw) File "init.py", line 1636, in _configure self.tk.call(_flatten((self._w, cmd)) + self._options(cnf)) _tkinter.TclError: unknown option "-image"

Meghansh-Maurya commented 1 year ago
  1. Use cv2.VideoCapture() from the OpenCV library to open the video file or capture video from a device.
  2. Read the video frames using video_capture.read() in a loop.
  3. Convert each frame from the default BGR color format to RGB using cv2.cvtColor(frame, cv2.COLOR_BGR2RGB).
  4. Create a PIL (Python Imaging Library) image from the converted frame using Image.fromarray(frame_rgb).
  5. If needed, you can resize the image using image.resize((width, height)).
  6. Convert the PIL image to a Tkinter-compatible format using ImageTk.PhotoImage(image). Assign it to a variable, let's say tk_image.
  7. Use canvas.create_image() to place the tk_image onto the canvas, specifying the desired coordinates.
  8. To draw additional items, such as a line, on top of the video frame, use the respective Tkinter canvas drawing methods, like canvas.create_line(). You can use canvas.lift(line) to bring the line above the video frame.
  9. While the video is playing, make sure to call canvas.lower(image) to keep the video frame below the other elements on the canvas.
zaynabtariq commented 1 year ago

Thank you! Should I be adding this code in the _load() method? And what lines of code should I be removing for this to work?

Meghansh-Maurya commented 1 year ago

no, you have to write code separately it is not included in Tkinter VideoPlayer. You have to write logic separately to play and pause the video. You will not get the in-built functions of VideoPlayer, Let me know if you face any problems.

zaynabtariq commented 1 year ago

Thanks! I got the video player working. However, I'm having trouble displaying the image on top. My image keeps flickering (if that's the right word?) as the video plays. My image also need to be moving along the video (its some sort of pointer). Is there a way to fix this issue?

PaulleDemon commented 1 year ago

Hey guys, I'll try to rewrite this library to inherit from Canvas instead of label when I get time.

Meghansh-Maurya commented 1 year ago

Are you using this repo or the steps I have given?

Meghansh-Maurya commented 1 year ago

Because I think the steps given by me is not having any flickering issue

zaynabtariq commented 1 year ago

@PaulleDemon Thanks! Would appreciate that. @Meghansh-Maurya I am using the steps given by you. I'm guessing its because its drawing the video frame, then image, then video frame which might cause this effect.

Meghansh-Maurya commented 1 year ago

Try canvas.image=yourimage This can work