finished pic viewer works and altered it to be in a bigger window along with image resizing

This commit is contained in:
Stephen Deaton 2024-07-09 16:19:25 -04:00
parent 96473af16b
commit fb79384114
2 changed files with 151 additions and 24 deletions

View File

@ -1,4 +1,4 @@
# if i was to give this to my mom and she didnt have the libraries would it work?
# https://www.youtube.com/watch?v=Y1rUga8Y5XY
import ttkbootstrap as ttk
from ttkbootstrap.constants import * # this imoports all ok to use?
@ -12,7 +12,7 @@ win.title("Image Viewer")
ttk.Style("superhero")
# size of the window
win.geometry("800x600")
win.geometry("1200x1000")
main_frame = ttk.Frame(win)
main_frame.pack()
@ -22,31 +22,13 @@ file_location = ""
img_no = 0
imgs = []
explorer_frame = ttk.Frame(main_frame)
explorer_frame.grid(row=0, column=0, padx=10, pady=(10, 0))
explorer_entery = ttk.Entry(explorer_frame, width=80)
explorer_entery.grid(row=0, column=0, columnspan=2, padx=(80, 0) )
image_frame = ttk.Frame(main_frame)
image_frame.grid(row=1,column=0)
btn_frame = ttk.Frame(main_frame)
btn_frame.grid(row=2,column=0)
no_label = ttk.Label(image_frame, font=("Arial" , 20, "bold"))
no_label.grid(row=0, column=4, pady=(10,0), sticky=NW)
image_label = ttk.Label(image_frame, width=100, text="")
image_label.grid(row=1, column=0, padx=(60, 0), pady=10, rowspan=10, columnspan=8)
# functions the guy put it around line 47 in the vid order?
# Picking the image folder
def explor_file():
global file_location
no_label.configure(text="")
file_location = filedialog.askdirectory(initialdir= '/', title="Selevt Folder")
explorer_entery.delete(0, END)
explorer_entery.insert(0, file_location)
@ -70,7 +52,7 @@ def set_img_list():
# RESIZE the images
w, h = img.size # Getting the current size (width and height) of the image
n_height = 400 #setting the new height that is a constant
n_height = 800 #setting the new height that is a constant
n_width = int((n_height/h) * w) # calculating the new width
img = img.resize((n_width, n_height))
imgs.append(ImageTk.PhotoImage(img))
@ -82,10 +64,50 @@ def set_img_list():
messagebox.showerror("No image", "No image in this directory, choose another to view images")
prev_btn = ttk.Button(btn_frame, text="Previous")
# Moving to the next image
def next_img():
global img_no
global imgs
no_of_img = len(imgs)
if img_no < no_of_img-1:
img_no+=1
image_label.configure(image=imgs[img_no])
no_label.configure(text=f"{img_no+1}/{no_of_img}")
# previous image button
def prev_img():
global img_no
global imgs
if img_no > 0:
img_no-=1
image_label.configure(image=imgs[img_no])
no_label.configure(text=f"{img_no+1}/{len(imgs)}")
explorer_frame = ttk.Frame(main_frame)
explorer_frame.grid(row=0, column=0, padx=10, pady=(10, 0))
explorer_entery = ttk.Entry(explorer_frame, width=80)
explorer_entery.grid(row=0, column=0, columnspan=2, padx=(80, 0) )
image_frame = ttk.Frame(main_frame)
image_frame.grid(row=1,column=0)
btn_frame = ttk.Frame(main_frame)
btn_frame.grid(row=2,column=0)
no_label = ttk.Label(image_frame, font=("Arial" , 20, "bold"))
no_label.grid(row=0, column=4, pady=(10,0), sticky=NW)
image_label = ttk.Label(image_frame, width=100, text="")
image_label.grid(row=1, column=0, padx=(60, 0), pady=10, rowspan=10, columnspan=8)
prev_btn = ttk.Button(btn_frame, text="Previous", command=prev_img)
prev_btn.grid(row=0, column=0, padx=10)
next_btn = ttk.Button(btn_frame, text=" Next ")
next_btn = ttk.Button(btn_frame, text=" Next ", command=next_img)
next_btn.grid(row=0, column=1, padx=10)
explorer_btn = ttk.Button(explorer_frame, text="Choose Location", command=explor_file)

View File

@ -0,0 +1,105 @@
# https://www.youtube.com/watch?v=Y1rUga8Y5XY
import ttkbootstrap as ttk
from ttkbootstrap.constants import * # this imoports all ok to use?
from tkinter import filedialog
from tkinter import messagebox
from PIL import ImageTk, Image
import os, os.path
win = ttk.Window()
win.title("Image Viewer")
ttk.Style("superhero")
# size of the window
win.geometry("800x600")
main_frame = ttk.Frame(win)
main_frame.pack()
# Variable NEED TO GO OVER THIS
file_location = ""
img_no = 0
imgs = []
explorer_frame = ttk.Frame(main_frame)
explorer_frame.grid(row=0, column=0, padx=10, pady=(10, 0))
explorer_entery = ttk.Entry(explorer_frame, width=80)
explorer_entery.grid(row=0, column=0, columnspan=2, padx=(80, 0) )
image_frame = ttk.Frame(main_frame)
image_frame.grid(row=1,column=0)
btn_frame = ttk.Frame(main_frame)
btn_frame.grid(row=2,column=0)
no_label = ttk.Label(image_frame, font=("Arial" , 20, "bold"))
no_label.grid(row=0, column=4, pady=(10,0), sticky=NW)
image_label = ttk.Label(image_frame, width=100, text="")
image_label.grid(row=1, column=0, padx=(60, 0), pady=10, rowspan=10, columnspan=8)
# functions the guy put it around line 47 in the vid order?
# Picking the image folder
def explor_file():
global file_location
file_location = filedialog.askdirectory(initialdir= '/', title="Selevt Folder")
explorer_entery.delete(0, END)
explorer_entery.insert(0, file_location)
set_img_list()
# extracting all the images in the folder
# RESIZING the images and saving them to a list
def set_img_list():
global imgs
global file_location
global img_no
global image_label
valid_images = [ ".jpg", ".gif", ".png,", ".tga"]
imgs = []
img_no =0
for file in os.listdir(file_location):
ext = os.path.splitext(file)[1]
if ext.lower() not in valid_images:
continue
img = Image.open(os.path.join(file_location, file))
# RESIZE the images
w, h = img.size # Getting the current size (width and height) of the image
n_height = 400 #setting the new height that is a constant
n_width = int((n_height/h) * w) # calculating the new width
img = img.resize((n_width, n_height))
imgs.append(ImageTk.PhotoImage(img))
if len(imgs) >= 1:
image_label.configure(image=imgs[img_no])
no_label.configure(text=f"{img_no+1}/{len(imgs)}")
else:
messagebox.showerror("No image", "No image in this directory, choose another to view images")
prev_btn = ttk.Button(btn_frame, text="Previous")
prev_btn.grid(row=0, column=0, padx=10)
next_btn = ttk.Button(btn_frame, text=" Next ")
next_btn.grid(row=0, column=1, padx=10)
explorer_btn = ttk.Button(explorer_frame, text="Choose Location", command=explor_file)
explorer_btn.grid(row=0, column=5, padx=10)
win.mainloop()