top of page

Convert Images w/Python Script

  • Writer: Jesse Olchawa
    Jesse Olchawa
  • Jul 12
  • 3 min read
Tool Banner
Tool Banner

Recently I've ran into a issue when using conversion sites to bulk convert a lot of my pictures to webp. Due to my large blogging habits I tend to run out of enough "credits" to convert the rest of all my pictures. Webp is a brilliant format, super optimised probably one of my top ones if I had a tier list (gif is number 1 moving funny images are hard to beat) so I made my own converter. It uses Python 3.11 but may run on lower versions too.

How the Script Looks
How the Script Looks

This script can:

  • Convert images solo or in bulk to desired format.

  • Auto prevents converting a image if it already matches the format.

  • Toggle on saving a folder to default selection to (useful on frequent conversions)


Theres two steps on getting it running: install PIL to your Python and paste my script into your Python terminal.


Installing PIL:

If you're on Windows like me you can open up command prompt and type in.

pip install pillow

If you're not then I suggest heading over to this page for more info on installing Pillow PIL Fork.


Image Converter Script:

Now for my script, it uses TKinter for a GUI interface. I prefer visual widgets and tools so TKinter was perfect for my needs.

Full Script Here

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()

Demo Video:

Heres a video of using the script. I'm running Python inside Visual Studio Code as I prefer its syncing capabilities with Unreal Engine.

Demo Video

Bibliography:

Useful links I used to build the tool and troubleshoot it.

Codecademy (2025). Getting Started with Image Processing in Python using Pillow. [online] Codecademy. Available at: https://www.codecademy.com/article/getting-started-with-image-processing-in-python-using-pillow.

Conyers, B. (2012). How to select a directory and store the location using tkinter in Python. [online] Stack Overflow. Available at: https://stackoverflow.com/questions/11295917/how-to-select-a-directory-and-store-the-location-using-tkinter-in-python.

GeeksforGeeks (2020). File Explorer in Python using Tkinter. [online] GeeksforGeeks. Available at: https://www.geeksforgeeks.org/python/file-explorer-in-python-using-tkinter/ [Accessed 12 Jul. 2025].

GeeksforGeeks (2021). Get Last N characters of a string Python. [online] GeeksforGeeks. Available at: https://www.geeksforgeeks.org/python/python-get-last-n-characters-of-a-string/ [Accessed 12 Jul. 2025].

TKDocs (2024). TkDocs Tutorial - Basic Widgets. [online] Tkdocs.com. Available at: https://tkdocs.com/tutorial/widgets.html#checkbutton [Accessed 12 Jul. 2025].

TKinter, J.W.L. updated T.G. started with (2022). Grid Layout Manager in Tkinter. [online] Python GUIs. Available at: https://www.pythonguis.com/tutorials/create-ui-with-tkinter-grid-layout-manager/.

TutorialPoint (n.d.). Python - Tkinter Checkbutton - Tutorialspoint. [online] www.tutorialspoint.com. Available at: https://www.tutorialspoint.com/python/tk_checkbutton.htm.

Ulhaq, M. (2012). How do I check if a directory exists in Python? [online] Stack Overflow. Available at: https://stackoverflow.com/questions/8933237/how-do-i-check-if-a-directory-exists-in-python.

Verma, A. (2019). Image Conversion (JPG ⇄ PNG/JPG ⇄ WEBP) with Python. [online] Medium. Available at: https://medium.com/@ajeet214/image-type-conversion-jpg-png-jpg-webp-png-webp-with-python-7d5df09394c9 [Accessed 12 Jul. 2025].

W3Schools (n.d.). Python - Global Variables. [online] www.w3schools.com. Available at: https://www.w3schools.com/python/python_variables_global.asp.


Comments


© 2025 Jesse Olchawa

The content provided on this website cannot be utilised for any AI training or data gathering purposes!

bottom of page