python - 使用 PIL/python 渲染数学文本

标签 python math python-imaging-library equation

我想将像“3/4”这样的字符串渲染成数学分数。

我可以通过渲染 HTML 页面并使用 Mathjax 来做到这一点,但这似乎有点过度。任何想法都非常感激。

最佳答案

enter image description here

#!/usr/bin/env python3

from PIL import Image, ImageDraw, ImageFont

text1 = u"Unicode maybe? \u00be \u2153 \u2157"
text2 = u"Maths? \u222c \u221e \u220f \u221a"

im  =  Image.new ( "RGB", (400,100), 0 )
draw  =  ImageDraw.Draw ( im )
unifont = ImageFont.truetype("/System/Library/Fonts//Menlo.ttc", 32)
draw.text((10,10), text1, font=unifont, fill=(255,255,255))
draw.text((10,55), text2, font=unifont, fill=(255,255,255))

# Save result
im.save("text.png")

请务必使用 Unicode 字体 - 例如 DejaVu.ttf 我使用 /System/Library/Fonts//Menlo.ttc 因为我使用的是 Mac .

我查了一下代码here .

关于python - 使用 PIL/python 渲染数学文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58678022/

相关文章:

使两条对角线都通过矩阵条目 (i,j) 的 Pythonic 方法

python - 使用 Python 进行多元多项式回归

Python:解析具有 7 位微秒到日期时间的时间戳字符串

Excel:赢得决胜局比赛的条件概率

c++ - 在给定时间内将频率从f1缓慢升高到f2的正弦波

python - 使用 PIL 缩放图像以保持透明度和颜色?

python - Django网站在cPanel中部署报错: Failed building wheel for Pillow, Reportlab

python - 您正在使用 staticfiles 应用程序,而没有将 STATIC_ROOT 设置设置为文件系统路径

javascript - 如何在 JavaScript 中执行整数除法并分别获取余数?

python - 使用PIL打开图片颜色不对