OSError: cannot open resource

10,383
import tkinter as tk  
from tkinter import ttk
from PIL import Image,ImageDraw,ImageFont
import matplotlib.font_manager as fm

root = tk.Tk()

def func_image():  
    image = Image.open(r'E:\side_300.png')  
    font_type_1 = ImageFont.truetype(fm.findfont(fm.FontProperties(family=combo.get())),18)
    draw = ImageDraw.Draw(image)  
    draw.text((50,50),text='Hello',fill='red',font=font_type_1)  
    image.show()  

fonts = list(set([f.name for f in fm.fontManager.ttflist]))
fonts.sort()

combo = ttk.Combobox(root,value=fonts)    
combo.pack()  

btn = ttk.Button(root,text='Click Me',command=func_image)  
btn.pack()

root.mainloop()
Share:
10,383
Bhavesh Mevada
Author by

Bhavesh Mevada

A keep learning Software Engineer who is enthusiast to create new things and debug the code especially in Python and JavaScript.

Updated on June 04, 2022

Comments

  • Bhavesh Mevada
    Bhavesh Mevada almost 2 years
    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...

  • Bhavesh Mevada
    Bhavesh Mevada about 5 years
    So, is there any module which helps me to return font file name.
  • Bhavesh Mevada
    Bhavesh Mevada about 5 years
    Yes it is possible.But, I want to provide a combo box for changing the font. Is there any module which helps me to get filename of a font?
  • pwxcoo
    pwxcoo about 5 years
    @BhaveshMevada you can DIY with os, just open C:\WINDOWS\Fonts path get all filename then you get all available font. You can customize anything for your want after it.
  • Rafael Barros
    Rafael Barros about 5 years
    Wow, congratulations! I looked all over for that solution, but couldn't find it. And, by the way, I've just read your solution and I think you can change these lines so your code becomes more readable: font_type_1 = ImageFont.truetype(fonts[combo.get()],18), fonts = dict([(f.name,f.fname) for f in fm.fontManager.ttflist]) and combo = ttk.Combobox(root,value=sorted(fonts.keys()))
  • Bhavesh Mevada
    Bhavesh Mevada about 5 years
    @RafaelBarros Thanks for your suggestion.
  • Rafael Barros
    Rafael Barros about 5 years
    I forgot to mention to remove this line fonts.sort() in the suggestion.
  • Bhavesh Mevada
    Bhavesh Mevada about 5 years
    @RafaelBarros I did it :)