user3925023
user3925023

Reputation: 687

Python - Tkinter - paste png trasparent image mouse cordinate and resize

my goal is to create an image editor for image augumentation. Basically what I'd like to do:

  1. run main code and load background image into canvas
  2. capture mouse click event for coordinates gathering on main canvas
  3. paste previous loaded PNG trasparent image on background-canvas
  4. possibility to resize with mouse pasted PNG transparent foreground image
  5. save pasted image relative cordinates (xmin,ymin,xmax,ymax)

I've started with this code:

import PIL.Image as p
from tkinter import *
from tkinter.filedialog import askopenfilename
import PIL.Image, PIL.ImageTk

if __name__ == "__main__":
    root = Tk()

    #setting up a tkinter canvas with scrollbars
    frame = Frame(root, bd=2, relief=SUNKEN)
    frame.grid_rowconfigure(0, weight=1)
    frame.grid_columnconfigure(0, weight=1)
    xscroll = Scrollbar(frame, orient=HORIZONTAL)
    xscroll.grid(row=1, column=0, sticky=E+W)
    yscroll = Scrollbar(frame)
    yscroll.grid(row=0, column=1, sticky=N+S)
    canvas = Canvas(frame, bd=0, xscrollcommand=xscroll.set, yscrollcommand=yscroll.set)
    canvas.grid(row=0, column=0, sticky=N+S+E+W)
    xscroll.config(command=canvas.xview)
    yscroll.config(command=canvas.yview)
    frame.pack(fill=BOTH,expand=1)

    #adding the image
    File = askopenfilename(parent=root, initialdir="C:/",title='Choose an image.')
    img = PIL.ImageTk.PhotoImage(PIL.Image.open(File))
    logo=PIL.ImageTk.PhotoImage(PIL.Image.open("C:\\logo1.png"))
    canvas.create_image(0,0,image=img,anchor="nw")
    canvas.config(scrollregion=canvas.bbox(ALL))

    #function to be called when mouse is clicked
    def printcoords(event):
        #outputting x and y coords to console

        img.paste(logo, (event.x,event.y))
        print (event.x,event.y)
    #mouseclick event
    canvas.bind("<Button 1>",printcoords)

    root.mainloop()`enter code here`

I'm getting this error while trying to paste image on background at line:

img.paste(logo, (0,0))

Error:

AttributeError: 'PhotoImage' object has no attribute 'load'

Do you have any suggestion? Many thanks

Update

Full traceback

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\xxx\Anaconda3\envs\yolo\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "c:/tensorflow1/Projects/xx/yolo_preproc/augumentation/yolo_trainer.py", line 46, in printcoords
    img.paste(logo, (0,0))
  File "C:\Users\xxx\Anaconda3\envs\yolo\lib\site-packages\PIL\ImageTk.py", line 165, in paste
    im.load()
AttributeError: 'PhotoImage' object has no attribute 'load'

Upvotes: 2

Views: 162

Answers (1)

jizhihaoSAMA
jizhihaoSAMA

Reputation: 12672

You should know that in functionImg.paste(argument_A,[argument_B]),

argument_A and Img should be a PIL.Image object.But In your code,

logo=PIL.ImageTk.PhotoImage(PIL.Image.open("C:\\logo1.png"))

logo is a PIL.ImageTk.PhotoImage object. So it will get error.

Now you should use logo=PIL.Image.open("C:\\logo1.png") instead of logo=PIL.ImageTk.PhotoImage(PIL.Image.open("C:\\logo1.png")).

Upvotes: 2

Related Questions