简体   繁体   中英

OSError: cannot open resource

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...

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()

ImageFont.truetype requires that you give it a filename. You're not giving it a filename, you're giving it the name of a font family. Tkinter's font.families() does not return filenames.

It seems font cannot be found by PIL.

Find your font file in your computer. In windows, it always in C:\\WINDOWS\\Fonts directory. select one and modify your line 9 like it:

font_type_1 = ImageFont.truetype("bahnschrift.ttf",18)

bahnschrift.ttf is just a sample on my computer, I am not sure it exists on your computer.

It does not work because you have to insert the font filename as the first argument here: ImageFont.truetype(str(combo.get()),18) .

If you try, for example, arial , you will succeed (if you have Arial installed on your computer, of course). Oh, and that function is case sensitive, so you have to write it in lowercase, because the filename is actually arial.ttf (you can drop the extension if want, by the way).

So, your combo box isn't working because when you choose a font named Courier New , for example, PIL won't find it, because its filename is cour.ttf . Unfortunately, you can't use that list of fonts from tkinter on ImageFont and I don't have a workaround for you in that case.

As I said, this might work, but you have to let go of your combo box: ImageFont.truetype('arial',18)

Before I go, one more important note: if you are working on a OS other than Windows, you have to type the full path to the font file.

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