PhotoImage not displaying the entire image

16 views Asked by At

I am attempting to load an image from the disk and annotate it with bounding boxes. All the functionality works however when an image is loaded only a small portion of the image is visible. How can I force the image to load fully?enter image description here

import tkinter as tk
from tkinter import filedialog
from PIL import Image, ImageTk


class ImageAnnotator:
def __init__(self, master):
    self.local_x = None
    self.local_y = None
    self.master = master
    self.master.title("Image Annotator")

    self.canvas = tk.Canvas(self.master)
    self.canvas.pack()

    self.image_path = filedialog.askopenfilename(title="Select an Image")
    self.image = Image.open(self.image_path)
    self.img_width, self.img_height = self.image.size
    
    self.photo = ImageTk.PhotoImage(self.image)
    self.canvas.create_image(0, 0, anchor=tk.NW, image=self.photo)

    self.boxes = []

    self.canvas.bind("<Button-1>", self.on_click)
    self.canvas.bind("<ButtonRelease-1>", self.on_release)

def on_click(self, event):
    self.local_x = event.x
    self.local_y = event.y

def on_release(self, event):
    box = [self.local_x, self.local_y, event.x, event.y]
    self.boxes.append(box)
    self.draw_box(box)

def draw_box(self, box):
    self.canvas.create_rectangle(box[0], box[1], box[2], box[3], outline="red")

def save_boxes(self):
    with open("boxes.txt", "w") as file:
        for box in self.boxes:
            x1, y1, x2, y2 = box
            file.write(f"{x1},{y1},{x2},{y2}\n")


if __name__ == "__main__":
    root = tk.Tk()
    screen_width = root.winfo_screenwidth()
    screen_height = root.winfo_screenheight()

    # Set the window size to be the same as the screen size
    root.geometry(f"{screen_width}x{screen_height}")
    app = ImageAnnotator(root)
    root.mainloop()
0

There are 0 answers