import tkinter as tk
from tkinter import ttk,font
from PIL import Image,ImageDraw,ImageFont
root = tk.Tk()
def func_image():
image = Image.open(r'E:\side_300.png')
font_type_1 = ImageFont.truetype(str(combo.get()),18)
draw = ImageDraw.Draw(image)
draw.text((50,50),text='Hello',fill='red',font=font_type_1)
image.show()
fonts=list(font.families())
fonts.sort()
combo = ttk.Combobox(root,value=fonts)
combo.pack()
btn = ttk.Button(root,text='Click Me',command=func_image)
btn.pack()
root.mainloop()
Output
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Mevada\AppData\Local\Programs\Python\Python37\lib\tkinter__init__.py", line 1702, in __call__return self.func(*args)
File "test.py", line 9, in func_image
font_type_1 = ImageFont.truetype(str(combo.get()),18)
File "C:\Users\Mevada\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\ImageFont.py", line 280, in truetype return FreeTypeFont(font, size, index, encoding, layout_engine)
File "C:\Users\Mevada\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\ImageFont.py", line 145, in __init__layout_engine=layout_engine)
OSError: cannot open resource
Thanks...