Python Tk display images using Canvas
examples/tk/tk_image.py
import tkinter as tk import os import sys from PIL import Image, ImageTk if len(sys.argv) != 2: exit(f"Usage: {sys.argv[0]} PATH_TO_IMAGE") file_path = sys.argv[1] app = tk.Tk() app.title('Show image') #img = tk.PhotoImage(file=file_path) # only seems to handle png and gif # Tried with png, jpg, gif img = ImageTk.PhotoImage(Image.open(file_path)) img_width = img.width() img_height = img.height() canvas = tk.Canvas(app, width = img_width, height = img_height) canvas.pack() img_on_canvas = canvas.create_image(img_width/2, img_height/2, image=img) app.mainloop()
examples/tk/tk_images.py
import tkinter as tk from tkinter import filedialog import os from PIL import Image, ImageTk file_path = None app = tk.Tk() app.title('Show image') def run_open(): global file_path file_path = filedialog.askopenfilename(filetypes=(("Image", "*.png *.jpg *.jpeg *.gif"), ("PNG", "*.png"), ("JPG", "*.jpg"), ("Any file", "*"),)) if file_path and os.path.isfile(file_path): global img img = ImageTk.PhotoImage(Image.open(file_path)) width = img.width() height = img.height() #print(width) #print(height) canvas.config(width=width, height=height) canvas.create_image(width/2, height/2, image=img) def run_exit(): print("exit") app.destroy() menubar = tk.Menu(app) menu1 = tk.Menu(menubar, tearoff=0) menu1.add_command(label="Open", underline=0, command=run_open) menu1.add_separator() menu1.add_command(label="Exit", underline=1, command=run_exit) menubar.add_cascade(label="File", underline=0, menu=menu1) app.config(menu=menubar) canvas = tk.Canvas(app, width = 600, height = 600) canvas.pack() app.mainloop()