【问题标题】:Render Text in an Image with Not Straight Borders在不直边框的图像中渲染文本
【发布时间】:2020-06-24 06:13:54
【问题描述】:

如何使我的文本呈现不直的边框,就像第二张图片一样? (两张图的字体不同,但边框的渲染方式值得关注。)

我的初始代码是

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont

from random import seed
from random import randint
import numpy as np
import os.path

#Returns the text size in terms of width and height.
def getSize(txt, font):
    testImg = Image.new('RGB', (1, 1))
    testDraw = ImageDraw.Draw(testImg)
    return testDraw.textsize(txt, font)

text = 'lemper'
fontname = 'arial.ttf'
fontsize= 25

font = ImageFont.truetype(fontname, fontsize)
width, height = getSize(text, font)

#Creates an image with white background of constant size.
img = Image.new('RGB', (100, 100), 'white')
d = ImageDraw.Draw( img)
d.text(get_xy_coordinates(text, font), text, fill='black', font=font)             
img.save("text_images/1.png")

【问题讨论】:

  • 使用您更喜欢的设计的另一种字体。字体之间的差异不是“使用另一种方式呈现它”的情况。实际的设计是不同的。
  • @usr2564301:我认为 OP 不是这个意思。她只是说字体无关紧要。

标签: python python-3.x image image-processing noise


【解决方案1】:

实现此效果的一种简单方法是使用“位移函数”(X(x, y), Y(x, y)) 将每个像素移动到附近的位置。这个函数应该是平滑的,幅度很小,有些不规则。例如,可以通过组合具有不可公度周期的三角函数来获得。

您必须实现重采样功能,使用双线性插值以获得良好的质量。

【讨论】:

  • 是否有任何 Python 库可以实现此功能?
  • @Cubilla:我不知道,我曾经自己编写过这样的东西;-) 但也许是 OpenCV 的 remap 函数。
猜你喜欢
  • 1970-01-01
  • 2019-06-22
  • 2014-02-03
  • 2021-11-04
  • 2011-07-21
  • 2020-06-24
  • 1970-01-01
  • 1970-01-01
  • 2013-05-02
相关资源
最近更新 更多