受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