BigSteve/PyGame/Picviewer/pcview.py

130 lines
3.4 KiB
Python

# 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?
from tkinter import filedialog
from tkinter import messagebox
from PIL import ImageTk, Image
import os, os.path
WIN_WIDTH = 1200
WIN_HEIGHT = 1000
win = ttk.Window()
win.title("Image Viewer")
ttk.Style("superhero")
# size of the window
win.geometry(f"{WIN_WIDTH}x{WIN_HEIGHT}")
main_frame = ttk.Frame(win)
main_frame.pack()
# Variable NEED TO GO OVER THIS
file_location = ""
img_no = 0
imgs = []
# 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="Select 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 = 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))
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")
# 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 ", command=next_img)
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()