【问题标题】:how to set watermark text on images in python [duplicate]如何在python中的图像上设置水印文本[重复]
【发布时间】:2018-09-10 03:46:00
【问题描述】:

我想在图片上设置水印文字...所以我尝试使用 PIL 库

def watermark_text(input_image,
                   output_image,
                   text, pos):
    photo = Image.open(input_image)
    drawing = ImageDraw.Draw(photo)

    color = (255, 180, 80)
    font = ImageFont.truetype("arial.ttf", 40)
    drawing.text(pos, text, fill=color, font=font)
    photo.show()
    photo.save(output_image)

if __name__ == '__main__':
    img = 'cat.jpg'
    watermark_text(img, 'cats.jpg',
                   text='Sample Location Text',
                   pos=(180, 200))

但我想要这种类型的交叉透明颜色的文本:

【问题讨论】:

标签: python python-imaging-library


【解决方案1】:

以下是如何使半透明水印显示为您要求的颜色:

from PIL import Image, ImageDraw, ImageFont
base = Image.open('cats.jpg').convert('RGBA')
width, height = base.size

# make a blank image for the text, initialized to transparent text color
txt = Image.new('RGBA', base.size, (255,255,255,0))

# get a font
fnt = ImageFont.truetype('arial.ttf', 40)
# get a drawing context
d = ImageDraw.Draw(txt)

x = width/2
y = height/2

# draw text, half opacity
d.text((x,y), "Hello", font=fnt, fill=(255,255,255,128))
txt = txt.rotate(45)

out = Image.alpha_composite(base, txt)
out.show()

【讨论】:

  • 非常感谢...知道如何横向设置文本
  • 哦,我忘了!有补充。
【解决方案2】:

您可以尝试如下旋转文本:

import ImageFont, ImageDraw, ImageOps

def watermark_text(input_image,
                   output_image,
                   text, pos):
    photo = Image.open(input_image)
    drawing = ImageDraw.Draw(photo)

    color = (255, 180, 80)
    font = ImageFont.truetype("arial.ttf", 40)
    drawing.text(pos, text, fill=color, font=font)
    angle=txt.rotate(17.5,  expand=1)
    photo.paste(ImageOps.colorize(w, (0,0,0), (255, 180, 80)), (242,60),  w)
    photo.show()
    photo.save(output_image)

您可以通过相应的 RGB 代码在 color 变量中使用正确的颜色。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多