Python compares the display effects of the same character in different fonts, and python Fonts

Source: Internet
Author: User

Python compares the display effects of the same character in different fonts, and python Fonts

In the openSUSE Chinese Forum, someone asked why the "charming" word "in his input method was displayed as" female "+" Yuan 」. I suspect it is a font problem, so I wrote a script using the python-fontconfig written by my friend and Pillow (a fork of PIL) When I was idle, use all fonts on the system that contain the word "yellow" to display the text and see which fonts are faulty.

(Updated) the script is as follows:

For Google Chrome/Chromium users, Note: If the copied Code contains uninterrupted spaces (0xa0), replace them manually.

#! /Usr/bin/env python3
# Vim: fileencoding = UTF-8

From PIL import Image, ImageDraw, ImageFont
Import fontconfig

Ch = 'hangzhou'
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,200 00
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 ')
Search for the body, and then paste it to the fonts.png file in the current directory. The process of searching for fonts takes a long time and you have to wait patiently. The final result is as follows:

Here I am, wenquanyi micron black, founder Wei Bei ref, and a Droid Sans Fallback font in which the "symbol" is incorrect. (I have three font files named "Droid Sans Fallback"...)>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.