python-3.x - Python-Tkinter-粘贴png透明图像鼠标坐标并调整大小

标签 python-3.x tkinter tkinter-canvas

我的目标是创建一个用于图像增强的图像编辑器。 基本上我想做的是:

  1. 运行主代码并将背景图像加载到 Canvas 中
  2. 捕获鼠标点击事件以在主 Canvas 上收集坐标
  3. 将之前加载的 PNG 透明图像粘贴到背景 Canvas 上
  4. 可以使用鼠标粘贴 PNG 透明前景图像来调整大小
  5. 保存粘贴图像相对坐标(xmin,ymin,xmax,ymax)

我从这段代码开始:

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`

我在尝试将图像粘贴到背景上的行时收到此错误:

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

错误:

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

你有什么建议吗? 非常感谢

更新

完整回溯

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'

最佳答案

你应该知道在函数Img.paste(argument_A,[argument_B])中,

argument_AImg 应该是 PIL.Image 对象。但是在您的代码中,

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

logo 是一个 PIL.ImageTk.PhotoImage 对象。 所以会报错。

现在您应该使用 logo=PIL.Image.open("C:\\logo1.png") 而不是 logo=PIL.ImageTk.PhotoImage(PIL.Image.open (“C:\\logo1.png”))

关于python-3.x - Python-Tkinter-粘贴png透明图像鼠标坐标并调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60577636/

相关文章:

python - 极地 python : interpolation in the groupby context

python - 从 Python 中调用 Robot Framework 文件

python - 为 Tkinter Scale 小部件设置 `orient` 关键字参数会导致 NameError : name 'HORIZONTAL' is not defined

python - 为什么使用并发线程时数据会重复

python - PyCharm 认为此 RegEx 具有 Duplicate character is character class。是不是bug?

python - tkinter 动态 OptionMenu 命令不起作用

python - (Tkinter)试图在循环中更新进度条值但程序崩溃了吗?

python - 如何在 tkinter 的 Canvas 项目上实现鼠标悬停回调?

python - 如何删除 Canvas 文本对象?

python - tkinter( python ): assign class method to a key