推荐学习书目
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 · Apr 22, 2020 · 2110 views
    This topic created in 2240 days ago, the information mentioned may be changed or developed.

    字体可以正常安装,所以我觉得应该不是字体问题 把自己的字体换成系统中有的 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 replies    2020-04-22 10:43:56 +08:00
    peterpei
        1
    peterpei  
    OP
       Apr 22, 2020
    补充一下:图片文件正常生成,可是图片里面没用字 QAQ
    a719114136
        2
    a719114136  
       Apr 22, 2020   ❤️ 1
    多半是字体问题,有可能字体中没有那几个字符,换个字体试试
    peterpei
        3
    peterpei  
    OP
       Apr 22, 2020
    @a719114136 感谢你的回复,但我在调试时输出的都是 112233 这类的字符,我个人认为不太可能,不过现在就去替换一个第三方字体试试,也算是缩小问题范围了 :)
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5654 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 03:33 · PVG 11:33 · LAX 20:33 · JFK 23:33
    ♥ Do have faith in what you're doing.