填充特定颜色的矩形内的水印 [英] A watermark inside a rectangle which filled the certain color

查看:74
本文介绍了填充特定颜色的矩形内的水印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在图片上添加水印.但是只是一个文本,而是一个填充有黑色和白色文本的矩形.

I want to add a watermark at a picture. But just a text, but a rectangle filled with the black color and a white text inside it.

目前,我只能输入文字:

For now, I only can put a text:

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

img = Image.open("in.jpg")

draw = ImageDraw.Draw(img)
font = ImageFont.truetype("/usr/share/fonts/truetype/ubuntu-font-family/Ubuntu-C.ttf", 66)
#font = ImageFont.truetype("Arialbd.ttf", 66)
draw.text((width - 510, height-100),"copyright",(209,239,8), font=font)
img.save('out.jpg')

推荐答案

这将在黑色矩形背景上绘制文本:

This will draw the text on a black rectangular background:

import Image
import ImageFont
import ImageDraw

img = Image.open("in.jpg")

draw = ImageDraw.Draw(img)
font = ImageFont.truetype(
    "/usr/share/fonts/truetype/ubuntu-font-family/Ubuntu-C.ttf", 66)
x, y = (width - 510, height-100)
# x, y = 10, 10
text = "copyright"
w, h = font.getsize(text)
draw.rectangle((x, y, x + w, y + h), fill='black')
draw.text((x, y), text, fill=(209, 239, 8), font=font)
img.save('out.jpg')

使用imagemagick,可以用

Using imagemagick, a better looking watermark could be made with

import Image
import ImageFont
import ImageDraw

font = ImageFont.truetype(
    "/usr/share/fonts/truetype/ubuntu-font-family/Ubuntu-C.ttf", 66)
text = "copyright"
size = font.getsize(text)
img = Image.new('RGBA', size=size, color=(0, 0, 0, 0))
draw = ImageDraw.Draw(img)
draw.text((0, 0), text, fill=(209, 239, 8), font=font)
img.save('label.jpg')

然后调用(如果需要,通过subprocess)类似

and then calling (through subprocess if you wish) something like

composite -dissolve 25% -gravity south label.jpg in.jpg out.jpg

或者如果您使用白色背景制作标签,

or if you make label with a white background,

composite -compose bumpmap -gravity southeast label.jpg in.jpg out.jpg

要从Python脚本中运行这些命令,可以使用subprocess这样:

To run these commands from within the Python script, you could use subprocess like this:

import Image
import ImageFont
import ImageDraw
import subprocess
import shlex

font = ImageFont.truetype(
    "/usr/share/fonts/truetype/ubuntu-font-family/Ubuntu-C.ttf", 66)
text = "copyright"
size = font.getsize(text)
img = Image.new('RGBA', size=size, color='white')
draw = ImageDraw.Draw(img)
draw.text((0, 0), text, fill=(209, 239, 8), font=font)
img.save('label.jpg')

cmd = 'composite -compose bumpmap -gravity southeast label.jpg in.jpg out.jpg'
proc = subprocess.Popen(shlex.split(cmd))
proc.communicate()

这篇关于填充特定颜色的矩形内的水印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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