Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ImageTk Paste
#14
The snapshot method opens a dialog, and if successful opens a second dialog. It opens two dialogs because you wrote the code that way. Then it proceeds to perform actions that don't make sense. Your problem is not with the image stuff, it is with everything else that has become a real mess because you've been thrashing away on this problem. Time to step back and get a simple example working.

I gutted your code, removing the video capture, and pared it down to a program that opens image files and pastes them on an existing image. The pasted image is centered where the cursor was when the mouse button was pressed inside the canvas object. There is a save button to write the resulting image to a file.
import tkinter as tk
from PIL import Image, ImageTk
from tkinter import filedialog

filetypes = (('PNG', '*.png'), ('JPEG', '*.jpg'), ('All', '*.*'))

class App:
    def __init__(self):
        self.image = None
        self.tk_image = None
        window = tk.Tk()
        self.canvas = tk.Canvas(window, width=360, height=360)
        self.canvas.pack()
        self.canvas.bind('<Button>', self.open)
        button = tk.Button(window, text="Save", command=self.save)
        button.pack(side=tk.LEFT, padx=10)
        window.mainloop()
 
    def open(self, event):
        """Open image file and paste to existing image"""
        filename = filedialog.askopenfile(mode='rb', filetypes=filetypes)
        if filename:
            # Open image file.  This creates a PIL image file object
            image = Image.open(filename)
            if self.image is None:
                # Set initial image
                self.image = image
            else:
                # Paste new image on existing image
                x = event.x - image.width//2
                y = event.y - image.height//2
                Image.Image.paste(self.image, image, (x, y))
            # Draw image on canvas.  Must convert to Tk image first
            self.tk_image = ImageTk.PhotoImage(self.image)
            self.canvas.delete(tk.ALL)
            self.canvas.create_image(180, 180,  image=self.tk_image)

    def save(self):
        """Save image to a file"""
        filename = filedialog.asksaveasfilename(filetypes=filetypes)
        if filename:
            self.image.save(filename)
  
if __name__ == "__main__":
    App()
I have never used PIL this way and I was surprised to see that paste is a function and not a method. You pass the base and pasted images as arguments. There may be a paste method for ImageTk objects, I haven't looked, but you should. You can use my short example to experiment around with that.

When you are trying something new you should write a short example instead of messing up existing code. The test program should be only as long as it has to be to test the idea. A nice side benefit of this type of development is you can keep all these programs in a folder, like a design notebook, and reference them later when you have questions about how to use different Python libraries. The short examples are also great for posting to the forum.
Reply


Messages In This Thread
ImageTk Paste - by KDog - May-31-2021, 09:47 PM
RE: ImageTk Paste - by bowlofred - May-31-2021, 10:29 PM
RE: ImageTk Paste - by Yoriz - May-31-2021, 10:31 PM
RE: ImageTk Paste - by KDog - Jun-01-2021, 03:07 PM
RE: ImageTk Paste - by Yoriz - Jun-01-2021, 04:40 PM
RE: ImageTk Paste - by KDog - Jun-01-2021, 09:19 PM
RE: ImageTk Paste - by Yoriz - Jun-01-2021, 10:16 PM
RE: ImageTk Paste - by deanhystad - Jun-01-2021, 10:20 PM
RE: ImageTk Paste - by KDog - Jun-02-2021, 11:28 AM
RE: ImageTk Paste - by Yoriz - Jun-02-2021, 11:39 AM
RE: ImageTk Paste - by KDog - Jun-02-2021, 12:54 PM
RE: ImageTk Paste - by deanhystad - Jun-02-2021, 05:06 PM
RE: ImageTk Paste - by KDog - Jun-02-2021, 09:42 PM
RE: ImageTk Paste - by deanhystad - Jun-03-2021, 03:27 AM
RE: ImageTk Paste - by KDog - Jun-27-2021, 11:07 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  What script to paste folders thenewcoder 1 678 Nov-29-2023, 09:40 AM
Last Post: Pedroski55
  PIL ImageTk issue with MATPLOTLIB garynewport 0 1,799 Jan-17-2023, 11:32 AM
Last Post: garynewport
  Please help me [copy and paste file from src to dst] midomarc 2 1,027 Nov-24-2022, 10:13 PM
Last Post: midomarc
  Cut and Paste Oshadha 3 2,449 Jan-20-2021, 04:27 PM
Last Post: spaceraiders
  copy paste file and re-name it asheru93 1 2,386 May-24-2019, 10:43 AM
Last Post: heiner55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020