Commit a749d99a by sikang

update script

parent 2fb61535
......@@ -2,41 +2,48 @@
import os
import sys
import random
from PIL import Image,ImageDraw,ImageFont
def add_text_to_image(image, text , mode):
#水印字体随机大小
font_size = random.randint(30,60)
from PIL import Image, ImageDraw, ImageFont
def add_text_to_image(image, text):
# 水印字体随机大小
font_size = random.randint(30, 60)
font = ImageFont.truetype('Arial.ttf', font_size)
# 添加背景
new_img = Image.new(mode, (image.size[0] * 3, image.size[1] * 3), (0, 0, 0, 0))
new_img = Image.new('RGBA', (image.size[0] * 3, image.size[1] * 3), (0, 0, 0, 0))
new_img.paste(image, image.size)
# 添加水印
font_len = len(text)
rgba_image = new_img.convert(mode)
text_overlay = Image.new(mode, rgba_image.size, (255, 255, 255, 0))
rgba_image = new_img.convert('RGBA')
text_overlay = Image.new('RGBA', rgba_image.size, (255, 255, 255, 0))
image_draw = ImageDraw.Draw(text_overlay)
#随机位置
for i in range(random.randint(0,100), rgba_image.size[0], font_len*font_size + font_size*2):
for j in range(random.randint(0,100), rgba_image.size[1], font_size*5):
# 随机位置
for i in range(random.randint(0, 100), rgba_image.size[0],
font_len * font_size + font_size * 2):
for j in range(random.randint(0, 100), rgba_image.size[1], font_size * 5):
image_draw.text((i, j), text, font=font, fill=(0, 0, 0, 50))
text_overlay = text_overlay.rotate(-45)
image_with_text = Image.alpha_composite(rgba_image, text_overlay)
# 裁切图片
image_with_text = image_with_text.crop((image.size[0], image.size[1], image.size[0] * 2, image.size[1] * 2))
image_with_text = image_with_text.crop(
(image.size[0], image.size[1], image.size[0] * 2, image.size[1] * 2))
return image_with_text
if __name__ == '__main__':
mode = 'RGB'
if sys.argv[1].endswith(".png"):
mode = "RGBA"
if __name__ == '__main__':
img = Image.open(sys.argv[1])
im_after = add_text_to_image(img, sys.argv[2],mode)
output = sys.argv[1].split(".");
output = output[0] + "_mark" + "." + output[1]
im_after = add_text_to_image(img, sys.argv[2])
path = os.path.dirname(sys.argv[1]) + "/mark_png/";
if not os.path.exists(path):
os.makedirs(path)
output = sys.argv[1].split("/");
output = path + output[len(output) - 1].split(".")[0] + "_mark" + ".png"
print(output)
im_after.save(output)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment