受List of all unicode's open/close brackets?啓發我試圖找到給定字體中所有unicode字形的列表,這些字形是彼此的反射。首先,我只需要能夠測試一個字形是否是另一個字形的反射。下面我有兩個不同的嘗試(我的render_char
函數的兩個不同的實現),但我無法使用任何一個將鏡像映射爲'('和')'。我怎樣才能做到這一點?在Python中測試一個字形是否是同一字體中的另一個字形的反射
from PIL import Image,ImageDraw,ImageFont
import freetype
import numpy as np
def render_char0(c):
# Based on https://github.com/rougier/freetype-py/blob/master/examples/hello-world.py
# Needs numpy (blech) and the image comes out the inverse of the way I expect
face = freetype.Face("/Library/Fonts/Verdana.ttf")
face.set_char_size(48*64)
face.load_char(c)
bitmap = face.glyph.bitmap
w,h = bitmap.width, bitmap.rows
Z = np.array(bitmap.buffer, dtype=np.ubyte).reshape(h,w)
return Image.fromarray(Z, mode='L').convert('1')
def render_char1(c):
# Based on https://stackoverflow.com/a/14446201/2829764
verdana_font = ImageFont.truetype("/Library/Fonts/Verdana.ttf", 20, encoding="unic")
text_width, text_height = verdana_font.getsize(c)
canvas = Image.new('RGB', (text_width+10, text_height+10), (255, 255, 255))
draw = ImageDraw.Draw(canvas)
draw.text((5,5), c, font = verdana_font, fill = "#000000")
return canvas
for render_char in [render_char0, render_char1]:
lparen = render_char('(')
rparen = render_char(')')
mirror = lparen.transpose(Image.FLIP_LEFT_RIGHT)
mirror.show()
rparen.show()
print mirror.tobytes() == rparen.tobytes() # False
我認爲rendereing是錯誤的方法。它取決於字體和*字體*知道如何渲染這種對稱性。我聽說unicode字符有這個規範。也許它是用他們的名字編碼的。 「左」和「右」「下標」。看看http://xahlee.info/comp/unicode_matching_brackets.html – User