在Python中测试一个字形是否是同一字体中的另一个字形的反射 [英] Testing in Python whether one glyph is a reflection of another in the same font

查看:461
本文介绍了在Python中测试一个字形是否是同一字体中的另一个字形的反射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有unicode开放/关闭括号列表的启发?我试图找到一个给定字体中的所有unicode字形的列表,彼此的反射。首先,我只需要能够测试一个字形是否是另一个字形的反射。下面我有两个不同的尝试(我的 render_char 函数的两个不同的实现),但是我无法使用任何一个来将'('和')'识别为镜像。

  from PIL import Image,ImageDraw,ImageFont 
import freetype
import numpy as np

def render_char0(c):
#基于https://github.com/rougier/freetype-py/blob/master/examples/hello-world.py
#需要numpy(blech)和图像出来的方式相反,我期望
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。缓冲区,dtype = np.ubyte).reshape(h,w)
返回Image.fromarray(Z,mode ='L')。convert('1')
$ b $ def render_char1 c):
#基于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)
返回画布

为render_char在[render_char0,render_char1]:
lparen = render_char('(')
rparen = render_char(')')
镜像= lparen.transpose(Image.FLIP_LEFT_RIGHT)

mirror.show()
rparen.show()
print mirror.tobytes()== rparen.tobytes()#False


解决方案

有一个名为 Unicode的纯文本数据库中的BidiMirroring.txt ,并带有所有镜像字符的列表。该文件很容易被程序解析。



当前网址是 http://www.unicode.org/Public/UNIDATA/BidiMirroring.txt



我不认为使用渲染字形可以可靠地工作。有很多原因,例如。 不是精确的镜像,就像字符间距,提示和反锯齿,也许是字体其他字符是旋转的而不是镜像的,如 在某些字体中,中文引号是。 p>

Inspired by List of all unicode's open/close brackets? I'm trying to find a list of all unicode glyphs in a given font that are reflections of each other. First I just need to be able to test whether one glyph is a reflection of another. Below I have two different attempts (two different implementations of my render_char function) but I'm not able to identify '(' and ')' as mirror images using either one. How can I do this?

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

解决方案

There is a text file called BidiMirroring.txt in the Unicode plain-text database with a list of all mirrored characters. That file is easy to parse by programs.

Current url is http://www.unicode.org/Public/UNIDATA/BidiMirroring.txt

I don't think using the rendered glyphs can work reliably. There's a lot of reasons why eg. ( and ) are no exact mirror images, like spacing around the character, hinting and anti-aliasing, maybe the font is slightly slanted, or maybe the font designer has just make the two brackets a bit different etc. Other characters are rotated, rather than mirrored, like " and " in some fonts, and the Chinese quotation marks and .

这篇关于在Python中测试一个字形是否是同一字体中的另一个字形的反射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆