【问题标题】:Render Text in an Image with Uneven Level of Saturation在饱和度不均匀的图像中渲染文本
【发布时间】:2020-06-24 05:19:46
【问题描述】:

如何以不均匀的饱和度渲染文本,就像第二张图片一样? (两张图片的字体不同,但饱和度值得关注。)

我的初始代码是

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")

【问题讨论】:

  • 你想改变饱和度还是连同字体和其他,因为两个图像不匹配?
  • @NagaKiran 两张图片的字体不同,但饱和度值得关注。
  • 饱和度是颜色的鲜艳度,黑色(以及白色和灰色阴影)根据定义是完全不饱和的,即没有饱和度。所以我认为这可能是错误的词。您可以用深灰色而不是黑色打印文本。您可以打印部分不透明的文本,这样一些不均匀的背景就会显示出来。
  • 如果您需要定制 OCR 引擎,请联系我

标签: python image image-processing noise distortion


【解决方案1】:

我希望饱和度不会带来任何变化,因为它是二值图像,我认为您正在寻找为图像添加一些噪点。

请查看imgaug库,根据需要添加或删除相关图像处理技术

import numpy as np
import cv2
import imgaug.augmenters as iaa


a = cv2.imread('fontimage.jpg')[None,:,:,:]
# Standard scenario: You have N RGB-images and additionally 21 heatmaps per

images = a.copy()
heatmaps = np.random.random(size=(1, 64, 64, 1)).astype(np.float32)

seq = iaa.Sequential([
    # Comment or uncomment the filters on your preference
    #iaa.GaussianBlur((0,3.0)),
    iaa.MultiplyHueAndSaturation((0.5, 1.5), per_channel=True),
    iaa.MultiplySaturation((0.1, 0.4)),
    iaa.SaltAndPepper(0.2)
])

images_aug, heatmaps_aug = seq(images=images, heatmaps=heatmaps)

输出:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-30
    • 1970-01-01
    • 1970-01-01
    • 2019-06-28
    • 2018-04-27
    • 2020-06-24
    • 1970-01-01
    相关资源
    最近更新 更多