how to save images with the save button on the tkinter in python how to save images with the save button on the tkinter in python tkinter tkinter

how to save images with the save button on the tkinter in python


To save image you have to use PIL.Image so don't assign PhotoImage to variable which you use to keep PIL.Image

edge = Image.fromarray(img)tk_edge = ImageTk.PhotoImage(edge)

and you have to use filename to save it

edge.save(filename)

Full working example

import tkinter as tkfrom tkinter import ttkfrom tkinter import filedialogfrom PIL import ImageTk, Image, ImageDrawimport cv2import numpy as np# --- functions ---def savefile():    filename = filedialog.asksaveasfile(mode='w', defaultextension=".jpg")    if not filename:        return    edge.save(filename)# --- main ---root = tk.Tk()img = cv2.imread('face_person1.jpg')edge = Image.fromarray(img)tk_edge = ImageTk.PhotoImage(edge)label = tk.Label(root, image=tk_edge)label.pack()button = tk.Button(root, text="save as", command=savefile)button.pack()root.mainloop()