Python implementation compares the display of the same character in different fonts _python

Source: Internet
Author: User

Someone in OpenSUSE Chinese forum asked his input method to play the "charming" "妩" the word Why Show as "female"+"yuan." Doubt is the font of the problem, so idle with a friend to write the python-fontconfig with Pillow (PiL of a fork) wrote a script, using the system all contain this "妩" word font to display the word, to see exactly which fonts have problems.

The (updated) script is as follows:

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

#!/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 ((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 fonts and render them to the Fonts.png file in the current directory. The process of looking for a font takes time and waits patiently. The final results are as follows:

Here I am, Wen Quan Shu, founder Weibei ref, some Droid Sans fallback font "妩" is not in the wrong shape. (I have three font files here called "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.