为什么我的truetype字体的大小11渲染不同于Windows? [英] why is my truetype font of size 11 rendering different than windows?

查看:241
本文介绍了为什么我的truetype字体的大小11渲染不同于Windows?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了说明:记事本打开了,字体选择为Arial,Size 11,仔细输入了This is a test这个字样,截取了一个截图:

p> original http://i53.tinypic.com/200skuv.png



输入以下Python代码并运行:

  import ImageFont,ImageDraw,Image 
im = Image.open(c:/textimg.png)#上面的图片

pilfont = ImageFont.truetype(arial.ttf,11)

compimg = Image.new(RGB,im.size,(255,255,255))
draw = ImageDraw.Draw(compimg)

draw.text(( 0,0),这只是一个测试,(0,0,0),font = pilfont)

compimg.save(c:/compimg.png)

然而结果令人失望:

伤心http://i56.tinypic.com/9h7x55.png



不仅是错误的大小,而且是还有一点点阴影,而记事本渲染是清晰的,并没有跨越像素边界。

我怎样才能让它像记事本一样渲染呢?我已经有了与pygame这个确切的问题,所以我想我在这里错过了一些关于TTF的基本理解。



更新:我再次尝试与pygame。它做同样的事情。它有一个选项可以关闭消除锯齿,但它看起来只是根据某个阈值,将任何像素进行抗锯齿处理。我得到的最接近的近似是使用大小15.代码是:
$ b $ pre $ py $ c $ pygfont = pygame.font.Font(rc: \windows\fonts\arial.ttf,15)
surf = pygfont.render(This is just a test,False,(0,0,0),(255,255,255))
pygame.image.save(surf,rc:\pygameimg.png)

和结果(记事本原件在上面比较):

杀我ME http://i56.tinypic.com/2r26mbs.png



Gah为什么我不能立即提供赏金? p>

更新:这里是比较所有的方法:

AIFEOIFEF http://i56.tinypic.com/51ybtg.png



PIL 15,然后记事本11,然后pygame 15反别名,然后pygame 15反别名。

PIL 15实际上具有正确的比例,它只是反锯齿。所以:为什么15比11?如何使它像Windows那样做呢? (和wtf是pygame在做什么?)解决方案

字体渲染是一个复杂而细微的过程,而一个已经实现了一个数字的时代。在你的情况下,PIL和Windows看起来不同,因为它们使用完全不同的字体渲染引擎。 Windows使用其内置的渲染,而PIL使用它编译的Freetype。

我不知道每个环境如何解释其尺寸参数,但即使你把它们解释得一样,渲染也只是不同而已。也许如果你解释一下为什么你需要和记事本相同的渲染,那么我们可以使用记事本来创建记事本,你会有问题的创意解决方案。


To wit: Notepad is opened, the font selected as "Arial, Size 11", the words "this is just a test" carefully entered, a screenshot taken:

original http://i53.tinypic.com/200skuv.png

The following Python code is entered and run:

import ImageFont, ImageDraw, Image
im = Image.open("c:/textimg.png") #the above image

pilfont = ImageFont.truetype("arial.ttf", 11)

compimg = Image.new("RGB", im.size, (255, 255, 255))
draw = ImageDraw.Draw(compimg)

draw.text((0,0), "this is just a test", (0,0,0), font=pilfont)

compimg.save("c:/compimg.png")

Yet the result is disappointingly different:

sad http://i56.tinypic.com/9h7x55.png

Not only is it the wrong size, but it's also slightly shaded, whereas the notepad rendering is crisp and crosses no pixel boundaries.

How can I get it to render like notepad does? I've had this exact issue with pygame as well, so I think I'm missing some fundamental understanding about TTF here.

UPDATE: I tried it with pygame again. It does the same thing. It does have an option to turn off anti-aliasing but it looks like it just lops off whatever pixels it would have antialiased based on some threshold. The closest approximation I got was using size 15. The code was:

pygfont = pygame.font.Font(r"c:\windows\fonts\arial.ttf", 15)
surf = pygfont.render("this is just a test", False, (0,0,0), (255,255,255))
pygame.image.save(surf, r"c:\pygameimg.png")

and the result (notepad original on top for comparison):

KILL ME http://i56.tinypic.com/2r26mbs.png

Gah why can't I offer a bounty right away?

UPDATE: here's comparing all the methods:

AIFEOIFEF http://i56.tinypic.com/51ybtg.png

PIL 15, then notepad 11, then pygame 15 anti-alias-off, then pygame 15 anti-alias-on.

PIL 15 actually has the right proportions, it's just anti-aliased. so: why 15 vs. 11? how to make it do it the same way windows does? (and wtf is pygame doing?)

解决方案

Font rendering is a complex and subtle process, and one that has been implemented a number of times. In your case, PIL and Windows look different because they are using completely different font rendering engines. Windows uses its built-in rendering, and PIL is using the Freetype it was compiled with.

I don't know how each environment interprets its "size" parameter, but even if you get them interpreted the same, the rendering will simply be different. The way to get the same pixels as Notepad is to launch Notepad and grab the screen.

Perhaps if you explain more about why you want the same rendering as Notepad, we'll have creative solutions to your problem.

这篇关于为什么我的truetype字体的大小11渲染不同于Windows?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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