Python实现对比不同字体中的同一字符的显示效果

yipeiwu_com6年前Python基础

有人在 openSUSE 中文论坛询问他的输入法打出的「妩媚」的「妩」字为什么显示成「女」+「元」。怀疑是字体的问题,于是空闲时用好友写的 python-fontconfig 配合 Pillow (PIL 的一个 fork)写了个脚本,使用系统上所有包含这个「妩」字的字体来显示这个字,看看到底是哪些字体有问题。

(更新后的)脚本如下:

Google Chrome / Chromium 用户请注意:如果复制得到的代码中含有不间断空格(0xa0),请手动替换下。

#!/usr/bin/env python3
# vim:fileencoding=utf-8

from PIL import Image, ImageDraw, ImageFont
import fontconfig

ch = '妩'
def get_fonts():
  ret = []
  for f in fontconfig.query():
    f = fontconfig.FcFont(f)
    if f.has_char(ch):
      ret.append((f.file, f.bestname))
  return ret

w, h = 800, 20000
image = Image.new('RGB', (w, h), 'white')
draw = ImageDraw.Draw(image)
pos = 0
w = 0
strs = ch
for fontfile, fontname in get_fonts():
  font = ImageFont.truetype(fontfile, 24)
  s = '%s: %s' % (fontname, strs)
  font_width, font_height = font.getsize(s)
  w = max((font_width, w))
  draw.text((10, pos), s, font=font, fill='black')
  pos += font_height
  h = pos

image = image.crop((0, 0, w+10, h))
image.save('fonts.png')
寻找字体,然后渲染到当前目录下的fonts.png文件中。寻找字体的过程挺花时间的,要耐心等待。最后结果如下:

我这里,文泉驿微米黑、方正魏碑ref、某个 Droid Sans Fallback 字体中「妩」字的字形不对。(我这里有三个字体文件都叫「Droid Sans Fallback」……)>

相关文章

python实现基本进制转换的方法

本文实例讲述了python基本进制转换的方法。分享给大家供大家参考。具体如下: # Parsing string with base into a number is easy nu...

CentOS 6.5中安装Python 3.6.2的方法步骤

前言 centos 是自带python的。但是版本稍微旧一些。搞python开发,肯定要用新一点的稳定版。所以,要升级一下python。本文将介绍在CentOS 6.5中安装Python...

python matplotlib 在指定的两个点之间连线方法

python matplotlib 在指定的两个点之间连线方法

为了找到matplotlib在两个点之间连线的方法真是费了好大功夫,最后还是决定用简单的 plt.plot 来解决。如果有好多对点,则可以通过循环实现连接,还可以用 plt.arrow...

对Pandas MultiIndex(多重索引)详解

创建多重索引 In [16]: df = pd.DataFrame(np.random.randn(3, 8), index=['A', 'B', 'C'], columns=ind...

Centos部署django服务nginx+uwsgi的方法

1.安装python3 yum -y install wget gcc make zlib-devel readline-devel bzip2-devel ncurses-dev...