图像“pyimage5”不存在[关闭]

问题描述 投票:0回答:0

我正在尝试向我的 GUI 中的按钮添加一个功能,该功能在单独运行时运行良好,但是当我将两者(GUI 和按钮的功能)集成在一起时,我得到一个错误,而我的 GUI 另一个窗口没有做任何事情。 我认为错误在 show_images() 的倒数第三行

from pathlib import Path
from tkinter import Tk, Canvas, Button, PhotoImage, Menu, filedialog
import cv2
import math
import numpy as np
from PIL import Image, ImageTk
import tkinter as tk

OUTPUT_PATH = Path(__file__).parent
ASSETS_PATH = Path(r"C:\College work\img stitching\Image-Stitching\src\build\assets\frame0")


def relative_to_assets(path: str) -> Path:
    return ASSETS_PATH / Path(path)

def open_new_window():
    class App:
        def __init__(self, master):
            self.master = master
            self.master.title("Image Viewer")
            self.master.geometry("800x600")

            self.images = []
            self.image_labels = []
            self.dragging = None
            self.drag_start = None
            self.zoom_scale=1.0

            self.canvas = tk.Canvas(self.master)
            self.canvas.pack(fill=tk.BOTH, expand=True)
            self.canvas.bind("<Button-1>", self.on_drag_start)
            self.canvas.bind("<B1-Motion>", self.on_drag_motion)
            self.canvas.bind("<ButtonRelease-1>", self.on_drag_release)
            self.canvas.bind("<MouseWheel>", self.on_zoom)

            menu = Menu(self.master)
            self.master.config(menu=menu)
            file_menu = Menu(menu)
            menu.add_cascade(label="File", menu=file_menu)
            file_menu.add_command(label="Open", command=self.open_images)

            # Create the flush button
            self.flush_button = tk.Button(self.master, text="CLEAR ALL", command=self.clear_all)
            self.flush_button.pack(side=tk.BOTTOM)

        def clear_all(self):
            self.images = []
            self.image_labels = []
            self.canvas.delete("all")

        def open_images(self):
            filenames = filedialog.askopenfilenames(filetypes=(("JPEG files", "*.jpg"), ("PNG files", "*.png"), ("All files", "*.*")))
            for filename in filenames:
                image = cv2.imread(filename)
                image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
                self.images.append(Image.fromarray(image))
                self.image_labels.append(None)
            self.show_images()

        def show_images(self):
            self.canvas.delete("all")
            for i, image in enumerate(self.images):
                width, height = image.size
                aspect_ratio = width / height
                max_width = self.canvas.winfo_width() / len(self.images)
                max_height = self.canvas.winfo_height()
                if aspect_ratio > 1:
                    width = min(max_width, width)
                    height = width / aspect_ratio
                else:
                    height = min(max_height, height)
                    width = height * aspect_ratio
                image = image.resize((int(width), int(height)), Image.LANCZOS)
                self.image_labels[i] = ImageTk.PhotoImage(image)
                x = i * (self.canvas.winfo_width() / len(self.images))
                y = (self.canvas.winfo_height() - height) / 2
                self.canvas.create_image(x, y, image=self.image_labels[i])
                self.original_width = self.canvas.winfo_width()
                self.original_height = self.canvas.winfo_height()

        def on_drag_start(self, event):
            item = self.canvas.find_closest(event.x, event.y)
            if item:
                x0, y0, x1, y1 = self.canvas.bbox(item)
                if x0 <= event.x <= x1 and y0 <= event.y <= y1:
                    self.dragging = item[0]
                    self.drag_start = (event.x, event.y)

        def on_drag_motion(self, event):
                if self.dragging:
                    dx = event.x - self.drag_start[0]
                    dy = event.y - self.drag_start[1]
                    self.canvas.move(self.dragging, dx, dy)
                    self.drag_start = (event.x, event.y)

        def on_drag_release(self, event):
                self.dragging = None
            
            
        def on_mousewheel(self, event):
                if event.delta > 0:
                    # Zoom in
                    self.canvas.scale("all", event.x, event.y, 1.2, 1.2)
                elif event.delta < 0:
                    # Zoom out
                    self.canvas.scale("all", event.x, event.y, 0.8, 0.8)

        def on_zoom(self, event):
                if event.delta > 0:
            # Zoom in
                    self.zoom_scale *= 1.1
                elif event.delta < 0:
            # Zoom out
                    self.zoom_scale /= 1.1
        # Clamp the zoom scale to reasonable values
                self.zoom_scale = max(0.1, min(10, self.zoom_scale))
        #   Update the canvas size
                self.canvas.config(width=math.ceil(self.original_width * self.zoom_scale), 
                height=math.ceil(self.original_height * self.zoom_scale))
    root = tk.Tk()
    app = App(root)
    root.mainloop()


window = Tk()

window.geometry("1302x809")
window.configure(bg="#373456")

canvas = Canvas(
    window,
    bg="#373456",
    height=809,
    width=1302,
    bd=0,
    highlightthickness=0,
    relief="ridge"
)
canvas.place(x=0, y=0)

image_image_1 = PhotoImage(file=relative_to_assets("image_1.png"))
image_1 = canvas.create_image(109.0, 63.0, image=image_image_1)

canvas.create_text(
    198.0,
    51.0,
    anchor="nw",
    text="Surface Modifications Technologies Pvt. Ltd.",
    fill="#FFFFFF",
    font=("MontserratRoman SemiBold", 20 * -1)
)

button_image_1 = PhotoImage(file=relative_to_assets("button_1.png"))
button_1 = Button(
    image=button_image_1,
    borderwidth=0,
    highlightthickness=0,
    command=lambda: print("button_1 clicked"),
    relief="flat"
)
button_1.place(x=34.0, y=305.0, width=343.0, height=409.0)

button_image_2 = PhotoImage(file=relative_to_assets("button_2.png"))
button_2 = Button(
    image=button_image_2,
    borderwidth=0,
    highlightthickness=0,
    command=lambda: print("button_2 clicked"),
    relief="flat"
)
button_2.place(x=912.0, y=305.0, width=343.0, height=409.0)

button_image_3 = PhotoImage(file=relative_to_assets("button_3.png"))
button_3 = Button(
    image=button_image_3,
    borderwidth=0,
    highlightthickness=0,
    command=open_new_window,
    relief="flat"
)
button_3.place(x=473.0, y=305.0, width=343.0, height=409.0)

canvas.create_text(
    377.0,
    170.0,
    anchor="nw",
    text="Image Stitching Tool",
    fill="#FFFFFF",
    font=("MontserratRoman ExtraBold", 50 * -1)
)

window.resizable(False, False)
window.mainloop()

它给了我一个错误:

Tkinter 回调异常 追溯(最近一次通话): 文件“C:\Users\Shreyans\AppData\Local\Programs\Python\Python310\lib kinter_init_.py”,第 1921 行,在 __call__ 中 返回 self.func(*args) 文件“c:\College work\img stitching\Image-Stitching\src uild\guiqt.py”,第 58 行,在 open_images 中 self.show_images() 文件“c:\College work\img stitching\Image-Stitching\src uild\guiqt.py”,第 77 行,在 show_images 中 self.canvas.create_image(x, y, image=self.image_labels[i]) 文件“C:\Users\Shreyans\AppData\Local\Programs\Python\Python310\lib kinter_init_.py”,第 2819 行,在 create_image 中 返回 self.create('image', args, kw) 文件“C:\Users\Shreyans\AppData\Local\Programs\Python\Python310\lib kinter_init.py”,第 2805 行,在 _create 返回 self.tk.getint(self.tk.call( _tkinter.TclError:图像“pyimage5”不存在`

该功能首先运行 idk y,我关闭该窗口然后出现 GUI 我按下按钮然后窗口出现我上传我的图像但它没有上传并抛出错误。

python python-3.x opencv tkinter
© www.soinside.com 2019 - 2024. All rights reserved.