【问题标题】:Dithering in text using PIL and truetype fonts使用 PIL 和 truetype 字体在文本中抖动
【发布时间】:2011-01-04 02:07:11
【问题描述】:

考虑以下代码: 从 PIL 导入 Image、ImageDraw、ImageFont

def addText(img, lTxt):
    FONT_SIZE = 10
    INTERLINE_DISTANCE = FONT_SIZE + 1
    font = ImageFont.truetype('arial.ttf', FONT_SIZE)
    lTxtImageHeight = INTERLINE_DISTANCE * len(lTxt)
    # create text image 
    lTxtImg = Image.new('RGBA', (img.size[1], lTxtImageHeight), 255)
    lTxtImgDraw = ImageDraw.Draw(lTxtImg, )
    for (i, line) in enumerate(lTxt):
      lTxtImgDraw.text((5, i * INTERLINE_DISTANCE), line, font=font, fill='#000000')
    # rotate text image
    lTxtImg = lTxtImg.rotate(90)
    # create new transparent image ret
    ret = Image.new('RGBA', (img.size[0] + lTxtImageHeight, img.size[1]), 255)
    # paste the image to ret
    ret.paste(img, (0,0))
    # paste the text to ret
    ret.paste(lTxtImg, (img.size[0], 0), lTxtImg)
    return ret

img = Image.open('in.png')
addText(img, ['lorem', 'ipsum', 'dolores']).save('out.png')

Here are the input and the output files 这是输入

input http://img16.imageshack.us/img16/8229/73936270.png

这是输出

output http://img94.imageshack.us/img94/531/outj.png

如您所见,输出图像在文本周围包含大量红色噪点。如何消除这种抖动?

【问题讨论】:

  • 首先,在旋转前后保存lTxtImg。比较图像:它们都有红色的“噪音”吗?顺便说一句,这种字体不是 Arial,它是衬线字体。你的操作系统是什么?

标签: python text python-imaging-library dithering


【解决方案1】:

我建议将中间文本图像写入文件(文本,然后是旋转的文本),以隔离伪影首先出现的位置。

另一种可能是 png 编码使用没有灰度值的调色板,因此这些红色是最接近的可用颜色。不过,我在 imageshack 上检查了文件的编码,似乎没问题,所以我认为这不是问题。

【讨论】:

    猜你喜欢
    • 2011-02-01
    • 2014-12-07
    • 2019-12-10
    • 2013-03-29
    • 2012-03-18
    • 1970-01-01
    • 2012-05-14
    • 1970-01-01
    • 2018-11-05
    相关资源
    最近更新 更多