Python implementations compare the display of the same character in different fonts

Source: Internet
Author: User
Someone in the OpenSUSE Chinese forum asked his input method played "charming" "flatter" the word why display as "female"+"yuan". Doubt is the problem of fonts, so when idle with friends write Python-fontconfig with Pillow (PIL a fork) wrote a script, using all the system contains the "flatter" word to display the word, to see exactly which fonts have a problem.

The (updated) script is as follows:

Google Chrome/chromium Users Note: If the copied code contains nonbreaking spaces (0xa0), please replace them manually.

#!/usr/bin/env Python3
# Vim:fileencoding=utf-8

From PIL import Image, Imagedraw, Imagefont
Import Fontconfig

ch = ' Flatter '
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 ((POS), S, Font=font, fill= ' black ')
pos + = Font_height
H = pos

Image = Image.crop ((0, 0, w+10, h))
Image.Save (' Fonts.png ')
Look for the font and render it to the Fonts.png file in the current directory. The process of looking for fonts takes time and waits patiently. The final results are as follows:

I am here, Wen Quan Shu, founder Weibei ref, some Droid Sans Fallback font "flatter" character in the wrong shape. (I have three font files called "droid Sans fallback" ... ) >

  • Related Article

    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.