Image Converter using TKinter + Pillow
JO00
python
6 months ago
3.8 kB
15
Indexable
import PIL.Image
from tkinter import *
from tkinter import filedialog
from tkinter import ttk
import os
view_label="/../Default/"
loc_look="/"
conv_to = [".webp", ".jpeg", ".png"]
def browseFiles():
#this gets location if ticked and looking there
looking_location="/"
if check_int.get() ==1:
looking_location=loc_look
#looking_location="C:/Users/Jesse/Downloads"
#actually looking for files
list_files = filedialog.askopenfilename(initialdir = looking_location, title = "Select a Image", filetypes = (("Image files", "*.png *.webp *.jpeg"), ("All files", "*.*")), multiple = True)
#now convert or try to at least
counter=1
for filename in list_files:
chosen_prefix=str(conv_to[cb.current()])
check_if_same=filename.endswith(chosen_prefix)
#fails if same does not waste convert
if check_if_same:
label_file_explorer.configure(text="File is same format did not convert ")
else:
label_file_explorer.configure(text="Begin Convert File: "+filename)
try:
strip_filename=filename[:filename.rfind('.')]
new_filename=strip_filename+str(chosen_prefix)
file_save_pref=chosen_prefix.removeprefix('.')
im = PIL.Image.open(filename).convert("RGB")
im.save(new_filename, file_save_pref)
string_success="Successfully converted "+str(counter) + " files!"
label_file_explorer.configure(text=string_success)
counter=counter+1
except:
label_file_explorer.configure(text="Failure to convert file")
def save_folder():
#looking folder and saving if successful
global loc_look
folder_sav=filedialog.askdirectory(initialdir = "/", title = "Select a Folder to Save")
if (os.path.isdir(folder_sav)):
view_label=(folder_sav[0:7])+"/../"+(folder_sav[-11:])
loc_look=folder_sav
save_label_location.configure(text=view_label)
else:
loc_look="/"
save_label_location.configure(text="/../Default/")
#print("doesnt exitst..")
#make gui
window = Tk()
window.title('Image Converter')
window.geometry("300x170")
window.config(background = "white")
#labels
label_file_explorer = Label(window, text = "Quick Use: Select Format, Select Images -> Convert",width = 40, height = 1, fg = "red")
button_explore = Button(window, text = "Select + Convert Images",command = browseFiles)
button_exit = Button(window, text = "Close",command = exit)
check_int = IntVar ()
check_save_fold=Checkbutton(window, text='Look in Saved?', variable=check_int, onvalue=1, offvalue=0)
save_label_location=Label(window, text = view_label,width = 25, height = 1, fg = "blue")
button_save_file = Button(window, text = "Save Folder for Looking to Select",command = save_folder)
# the dropdown file type
cb = ttk.Combobox(window, values=conv_to)
label_ontop=Label(window, text = "Convert selected images to:", width = 40, height = 1, fg = "blue")
label_priorconv=Label(window, text = "Select images to convert by pressing button below", width = 40, height = 1, fg = "blue")
cb.set(conv_to[0])
#location in gui
label_file_explorer.grid(column = 1, row = 1, sticky="w", padx=3)
label_ontop.grid(column = 1, row = 2, sticky="w")
label_priorconv.grid(column = 1, row = 4, sticky="w")
button_explore.grid(column = 1, row = 5, sticky="WENS", padx=3)
cb.grid(column = 1,row = 3, sticky="WENS", padx=3)
check_save_fold.grid(column = 1,row = 6, sticky="W", padx=3)
save_label_location.grid(column = 1,row = 6, sticky="E", padx=3)
button_save_file.grid(column = 1, row = 7, sticky="WENS", padx=3)
window.mainloop()
Editor is loading...
Leave a Comment