How to put image uploaded in tkinter into a function?

Solution 1:

Firstly import filedialog and PIL:

from tkinter import filedialog
from PIL import Image

Now use a variable path (or anything) to define the path that is returned when you choose inside of a GUI.

path = filedialog.askopenfilename(initialdir='/Downloads', title='Select Photo', filetypes=(('JPEG files', '*.jpg'), ('PNG files', '*.png')))

You can use any filetype you want by specifying it in the argument there with the title and '*.extension'. NOTE THAT IT IS CASE-SENSITIVE. Now your variable path has the path for the image and all you have to do is open it with PIL using

img = Image.open(path)

Just like you did in your code