Lau*_*ell 10 fonts image python-imaging-library python-3.x
我正在尝试编写一个将文本放到图像上的程序,我正试图绕过PIL并遇到错误:OSError:无法打开资源.这是我的第一个python程序,如果错误很明显,请道歉.
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
im = Image.open("example.jpg")
font_type = ImageFont.truetype("Arial.ttf", 18)
draw = ImageDraw.Draw(im)
draw.text(xy=(50, 50), text= "Text One", fill =(255,69,0), font = font_type)
im.show()
Run Code Online (Sandbox Code Playgroud)
我收到错误:
Traceback (most recent call last):
File "C:\Users\laurence.maskell\Desktop\attempt.py", line 7, in <module>
font_type = ImageFont.truetype("Arial.ttf", 18)
File "C:\Python34\lib\site-packages\PIL\ImageFont.py", line 259, in truetype
return FreeTypeFont(font, size, index, encoding, layout_engine)
File "C:\Python34\lib\site-packages\PIL\ImageFont.py", line 143, in __init__
self.font = core.getfont(font, size, index, encoding,
layout_engine=layout_engine)
OSError: cannot open resource
Run Code Online (Sandbox Code Playgroud)
Uzz*_*der 15
我固定的使用问题默认字体。
font = ImageFont.load_default()
Run Code Online (Sandbox Code Playgroud)
如果您只需要放置一些文本(字体样式对您来说无关紧要),那么这可能是一个简单的解决方案。
font = ImageFont.load_default()
draw = ImageDraw.Draw(pil_img)
draw.text(( 20, 32), "text_string", (255,0,0), font=font)
Run Code Online (Sandbox Code Playgroud)
小智 9
from PIL import Image,ImageDraw,ImageFontim = Image.open("mak.png")
font_type = ImageFont.truetype("arial.ttf", 18)
draw = ImageDraw.Draw(im)
draw.text(xy=(120, 120), text= "download the font that you wanna use", fill =(255,69,0), font = font_type)
im.show()
Run Code Online (Sandbox Code Playgroud)
它的“arial.ttf”不是“Arial.ttf”
我在带有PIL 5.3.0的Windows 10 Pro上也遇到了此问题。
在我的机器上,该错误是由非ASCII字体文件名引起的。如果将字体名称更改为仅包含ASCII字符,则可以打开字体而没有任何错误。
编辑(2019-07-29):这确实是ImageFont.truetype()method 的错误,并且已在此pull request中修复。
对于 Linux,我使用:
$ locate .ttf
/usr/share/fonts/truetype/ubuntu-font-family/Ubuntu-B.ttf
/usr/share/fonts/truetype/ubuntu-font-family/Ubuntu-BI.ttf
/usr/share/fonts/truetype/ubuntu-font-family/Ubuntu-C.ttf
/usr/share/fonts/truetype/ubuntu-font-family/Ubuntu-L.ttf
/usr/share/fonts/truetype/ubuntu-font-family/Ubuntu-LI.ttf
/usr/share/fonts/truetype/ubuntu-font-family/Ubuntu-M.ttf
/usr/share/fonts/truetype/ubuntu-font-family/Ubuntu-MI.ttf
/usr/share/fonts/truetype/ubuntu-font-family/Ubuntu-R.ttf
/usr/share/fonts/truetype/ubuntu-font-family/Ubuntu-RI.ttf
/usr/share/fonts/truetype/ubuntu-font-family/UbuntuMono-B.ttf
/usr/share/fonts/truetype/ubuntu-font-family/UbuntuMono-BI.ttf
/usr/share/fonts/truetype/ubuntu-font-family/UbuntuMono-R.ttf
/usr/share/fonts/truetype/ubuntu-font-family/UbuntuMono-RI.ttf
Run Code Online (Sandbox Code Playgroud)
它实际上返回的远不止于此!
然后我在 Stack Overflow 上发布了 python 代码:
插入由以下命令返回的字体名称“Ubuntu-R.ttf” locate:
color_palette = [BLUE, GREEN, RED]
image_w=200
image_h=200
region = Rect(0, 0, image_w, image_h)
imgx, imgy = region.max.x+1, region.max.y+1
image = Image.new("RGB", (imgx, imgy), WHITE)
draw = ImageDraw.Draw(image)
vert_gradient(draw, region, gradient_color, color_palette)
#image.text((40, 80),"No Artwork",(255,255,255))
#font = ImageFont.truetype(r'C:\Users\System-Pc\Desktop\arial.ttf', 40)
#font = ImageFont.load_default()
font = ImageFont.truetype("Ubuntu-R.ttf", int(float(image_w) / 6))
draw.text((int(image_w/12), int(image_h / 2.5)), "No Artwork", \
fill=(0,0,0), font=font)
image.show()
Run Code Online (Sandbox Code Playgroud)
瞧!现在,当我正在播放的音乐文件中没有可显示的图像时,我可以显示图像: