Advertisement
Najeebsk

PICTURE-VIEWER2.py

Feb 29th, 2024 (edited)
1,184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.52 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import filedialog
  3. from PIL import Image, ImageTk
  4. import glob
  5.  
  6. def insertfiles(folder):
  7.     for filename in glob.glob(folder + "/*.png"):
  8.         lst.insert(tk.END, filename)
  9.     for filename in glob.glob(folder + "/*.jpg"):
  10.         lst.insert(tk.END, filename)
  11.     for filename in glob.glob(folder + "/*.bmp"):
  12.         lst.insert(tk.END, filename)    
  13.  
  14. def showimg(event):
  15.     n = lst.curselection()
  16.     if n:
  17.         filename = lst.get(n[0])
  18.         img = Image.open(filename)
  19.         w_img, h_img = img.size
  20.        
  21.         # Get the dimensions of the window
  22.         w_win = root.winfo_width()
  23.         h_win = root.winfo_height()
  24.        
  25.         # Calculate the aspect ratio of the image and window
  26.         aspect_ratio_img = w_img / h_img
  27.         aspect_ratio_win = w_win / h_win
  28.        
  29.         if aspect_ratio_img > aspect_ratio_win:
  30.             # Scale the image based on width
  31.             new_w = w_win
  32.             new_h = int(w_win / aspect_ratio_img)
  33.         else:
  34.             # Scale the image based on height
  35.             new_h = h_win
  36.             new_w = int(h_win * aspect_ratio_img)
  37.        
  38.         img = img.resize((new_w, new_h), Image.LANCZOS)  # Use LANCZOS for resampling
  39.         photo = ImageTk.PhotoImage(img)
  40.         canvas.config(width=new_w, height=new_h)
  41.         canvas.create_image(0, 0, image=photo, anchor=tk.NW)
  42.         canvas.image = photo
  43.  
  44. def browse_folder():
  45.     folder_selected = filedialog.askdirectory()
  46.     if folder_selected:
  47.         lst.delete(0, tk.END)  # Clear the listbox
  48.         insertfiles(folder_selected)
  49.  
  50. root = tk.Tk()
  51. root.title("NAJEEB IMAGE VIEWER")
  52. #root.configure(bg="Green")  # set background color
  53. root.geometry("800x600+300+50")
  54.  
  55. # Create a frame to hold the listbox and scrollbar
  56. frame = tk.Frame(root)
  57. frame.pack(side="left", fill="y")
  58.  
  59. # Create a vertical scrollbar
  60. scrollbar = tk.Scrollbar(frame, orient="vertical")
  61.  
  62. # Create the listbox with the scrollbar attached
  63. lst = tk.Listbox(frame, width=15, yscrollcommand=scrollbar.set)
  64. scrollbar.config(command=lst.yview)
  65.  
  66. # Pack the listbox and scrollbar
  67. lst.pack(side="left", fill="both", expand=True)
  68. scrollbar.pack(side="right", fill="y")
  69.  
  70. lst.bind("<<ListboxSelect>>", showimg)
  71.  
  72. # Insert files from the selected folder
  73. insertfiles(".")
  74.  
  75. # Add a button to browse for a folder
  76. browse_button = tk.Button(root, text="Browse Folder", command=browse_folder)
  77. browse_button.pack()
  78.  
  79. canvas = tk.Canvas(root)
  80. canvas.pack(fill=tk.BOTH, expand=True)
  81.  
  82. root.mainloop()
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement