简体   繁体   中英

How to save a .png file through python in tkinter by pressing a button

Guys I wanted to know how to save a png file to a specific path through tkinter I have a button and when I click in it I want it to save a specific png file in a path that I write in the entry u can look at my code here

from tkinter import *
from tkinter import filedialog
from tkinter import*
from tkinter.ttk import *
import pyautogui
from PIL import ImageTk,Image
import time
import os.path
def callback(event):
    w1 = e1.get()
    h1 = e2.get()
    im = pyautogui.screenshot('my_screenshot.png',region=(0,0, w1, h1))
    tru()
def save():
    save_path =e3.get()
    #if name_of_file is True:
    #name_of_file=name_of_file+1
    completeName = os.path.join(save_path, "my_screenshot.png")
window = tkinter.Tk()
window.wm_iconbitmap('Scissors-black.ico')
window.title("Screenshot Taker")
#window.geometry("500x500")
frame = LabelFrame(window,text="The Screenshot")
frame.grid(row=4,column=None)
e1 = Entry(window)
e2 = Entry(window)
l1=Label(window,text="Width")
l2=Label(window,text="Height")
l1.grid(row=0,column=0, pady = 2)
l2.grid(row=1,column=0, pady = 2)
e1.grid(row = 0, column = 1, pady = 2)
e2.grid(row = 1, column = 1, pady = 2)
b1 = Button(text="Save",command=save)
b1.grid(row=3, column=0, pady = 2)
e3 = Entry(window)
e3.grid(row =3, column=1, pady = 2)
L=window.bind("<Return>",callback)
def tru():
    my_img = ImageTk.PhotoImage(Image.open("my_screenshot.png"))
    l3=Label(frame,image=my_img)
    l3.grid(row=4,column=None)
    window.mainloop()


#label = tkinter.Label(window,text = "Press Enter to take a screenshot").pack()

You can save image by Image module from PIL library

#import image module
from PIL import Image
#Create image object
image = Image.open(r"path_to_image")#type the path
image.save("Example.png") #type the location to be saved

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM