V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
peterpei
V2EX  ›  Python

PIL 换个字体, 就无法生成图片文件了

  •  1
     
  •   peterpei · 2020-04-22 09:24:10 +08:00 · 1614 次点击
    这是一个创建于 1436 天前的主题,其中的信息可能已经有所发展或是发生改变。

    字体可以正常安装,所以我觉得应该不是字体问题 把自己的字体换成系统中有的 arial.ttf 就没问题了,既不是字体的问题,也不是程序的问题,请问哪里出错了呢

    import os , pyautogui , os.path , time
    from PIL import ImageFont ,Image, ImageDraw
    
    scriptpath = os.path.dirname(__file__)
    
    userfilename = os.path.join(scriptpath, 'selections.PNG')
    
    font1 = os.path.join(scriptpath, '1.ttf')
    
    im = Image.open(userfilename , mode="r")
    
    font = ImageFont.truetype(font1, 16)
    
    draw = ImageDraw.Draw(im)
    
    draw.text((0 , 0) , "1233" , font = font , fill=(0,0,0) )
    
    im.save(userfilename+'.JPG', "PNG")
    
    file1 = os.path.join(scriptpath, 'selections.PNG.JPG')
    
    3 条回复    2020-04-22 10:43:56 +08:00
    peterpei
        1
    peterpei  
    OP
       2020-04-22 09:24:47 +08:00
    补充一下:图片文件正常生成,可是图片里面没用字 QAQ
    a719114136
        2
    a719114136  
       2020-04-22 10:11:34 +08:00   ❤️ 1
    多半是字体问题,有可能字体中没有那几个字符,换个字体试试
    peterpei
        3
    peterpei  
    OP
       2020-04-22 10:43:56 +08:00
    @a719114136 感谢你的回复,但我在调试时输出的都是 112233 这类的字符,我个人认为不太可能,不过现在就去替换一个第三方字体试试,也算是缩小问题范围了 :)
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5318 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 09:25 · PVG 17:25 · LAX 02:25 · JFK 05:25
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.