from tkinter import *
from PIL import Image, ImageTk, ImageDraw,ImageFont
from tkinter import filedialog
import sys
def msopen():
global file_path
file_path = filedialog.askopenfilename()
global root,canvas,img
pilImage = Image.open(file_path)
img = ImageTk.PhotoImage(pilImage)
canvas.create_image(0,0,image=img,anchor = NW)
def mstext():
global text
text = entry.get()
def msaddtext():
global entry
window = Toplevel(root)
window.geometry('400x200')
window.title('Добавить текст')
entry = Entry(window,width = 400)
b1 = Button(window,text="+",command=mstext)
b1.place(x = 0, y = 50)
entry.place(x = 0, y =0)
def mssave():
global file_path
im = Image.open(file_path)
draw = ImageDraw.Draw(im)
unicode_font = ImageFont.truetype("arial.ttf", 10)
draw.text((10,10),text,font=unicode_font, fill=(255,255,255,128))
del draw
file = filedialog.asksaveasfile(defaultextension='.png')
im.save(file.name, "PNG")
global root,canvas,img
pilImage = Image.open(file.name)
img = ImageTk.PhotoImage(pilImage)
canvas.create_image(0,0,image=img,anchor = NW)
pass
def mexit():
sys.exit(0)
root = Tk()
root.geometry('1000x1000')
mainmenu = Menu(root)
root.config(menu=mainmenu)
filemenu = Menu(mainmenu, tearoff=0)
filemenu.add_command(label="Загрузить изображение",command=msopen)
filemenu.add_command(label="Добавить текст",command=msaddtext)
filemenu.add_command(label="Cохранить изображение",command=mssave)
filemenu.add_command(label="Выход",command=mexit)
mainmenu.add_cascade(label="Команды", menu=filemenu)
canvas = Canvas(root, width=999, height=999, bg='white')
canvas.place(x = 0 , y = 0)
img = PhotoImage(file='')
file_path = None
text = None
entry = None
root.mainloop()
|