|
| 1 | +import tkinter as tk |
| 2 | +from tkinter import filedialog |
| 3 | +from PIL import Image, ImageTk |
| 4 | +import sys |
| 5 | + |
| 6 | +# Define the image file path |
| 7 | +image_path = "data_image.png" |
| 8 | + |
| 9 | +def open_image(): |
| 10 | + file_path = filedialog.askopenfilename(filetypes=[("Image files", "*.png *.jpg *.jpeg *.gif *.bmp *.ppm *.pgm *.tif *.tiff")]) |
| 11 | + if file_path: |
| 12 | + display_image(file_path) |
| 13 | + |
| 14 | +def exit_viewer(): |
| 15 | + window.destroy() |
| 16 | + |
| 17 | +def display_image(image_path, resize_width=None, resize_height=None): |
| 18 | + # Create a new window |
| 19 | + window = tk.Tk() |
| 20 | + window.title("Pixel Data Image Viewer") |
| 21 | + |
| 22 | + # Set the background color to black |
| 23 | + window.configure(bg="black") |
| 24 | + |
| 25 | + # Create a menu bar |
| 26 | + menubar = tk.Menu(window) |
| 27 | + window.config(menu=menubar) |
| 28 | + |
| 29 | + # File menu |
| 30 | + file_menu = tk.Menu(menubar, tearoff=0) |
| 31 | + menubar.add_cascade(label="File", menu=file_menu) |
| 32 | + |
| 33 | + # Load and display the image with transparency |
| 34 | + image = Image.open(image_path) |
| 35 | + |
| 36 | + if resize_width and resize_height: |
| 37 | + image = image.resize((resize_width, resize_height)) |
| 38 | + elif resize_width: |
| 39 | + aspect_ratio = image.width / image.height |
| 40 | + new_height = int(resize_width / aspect_ratio) |
| 41 | + image = image.resize((resize_width, new_height)) |
| 42 | + elif resize_height: |
| 43 | + aspect_ratio = image.width / image.height |
| 44 | + new_width = int(resize_height * aspect_ratio) |
| 45 | + image = image.resize((new_width, resize_height)) |
| 46 | + |
| 47 | + # Create a transparent image with the same size |
| 48 | + transparent_image = Image.new("RGBA", image.size, (255, 255, 255, 0)) |
| 49 | + |
| 50 | + # Paste the loaded image onto the transparent image |
| 51 | + transparent_image.paste(image, (0, 0), image) |
| 52 | + |
| 53 | + photo = ImageTk.PhotoImage(transparent_image) |
| 54 | + label = tk.Label(window, image=photo, bg="black") |
| 55 | + label.image = photo # Keep a reference to the image |
| 56 | + label.pack() |
| 57 | + |
| 58 | + window.mainloop() |
| 59 | + |
| 60 | +if __name__ == "__main__": |
| 61 | + if len(sys.argv) == 2: |
| 62 | + image_path = sys.argv[1] |
| 63 | + |
| 64 | + # You can specify the desired width and height for resizing |
| 65 | + display_image(image_path, resize_width=700, resize_height=600) |
0 commit comments