1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
import os import sys import math import shutil import tinify
from PIL import Image, ImageFont, ImageDraw, ImageEnhance, ImageChops
opacity = 0.2 padding = 150 angle = 30 quality = 80
word = 'itcoca.cn' word_size = 50 word_line = 1.2 word_color = '#8B8B1B' word_font = 'C:\\Windows\\Fonts\\timesbd.ttf'
tinify.key = "xxx"
def gen_mark(): """ 生成单个水印图片 """
width = len(word) * word_size height = round(word_size * word_line) mark = Image.new(mode='RGBA', size=(width, height))
mark_draw = ImageDraw.Draw(im=mark) mark_draw.text(xy=(0, 0), text=word, fill=word_color, font=ImageFont.truetype(word_font, size=word_size)) del mark_draw
mark_bg = Image.new(mode='RGBA', size=mark.size) diff = ImageChops.difference(mark, mark_bg) del mark_bg bbox = diff.getbbox() if bbox: mark = mark.crop(bbox)
alpha = ImageEnhance.Brightness(mark.split()[3]).enhance(opacity) mark.putalpha(alpha)
return mark
def add_mark(image_path): """ 为图片添加水印 """
im = Image.open(image_path) im.thumbnail((800, 99999))
c = int(math.sqrt(im.size[0] * im.size[0] + im.size[1] * im.size[1])) mark = gen_mark() mark2 = Image.new(mode='RGBA', size=(c, c))
y, idx = 0, 0 while y < c: x = -int((mark.size[0] + padding) * 0.5 * idx) idx = (idx + 1) % 2
while x < c: mark2.paste(mark, (x, y)) x = x + mark.size[0] + padding y = y + mark.size[1] + padding
mark2 = mark2.rotate(angle)
if im.mode != 'RGBA': im = im.convert('RGBA') im.paste(mark2, (int((im.size[0] - c) / 2), int((im.size[1] - c) / 2)), mask=mark2.split()[3]) del mark2
if im: if os.path.splitext(image_path)[1] != '.png': im = im.convert('RGB') im.save(image_path, optimize=True, quality=quality) print('Success.') else: print('Failed.')
def bak_image(image_path): arr = image_path.split('.') arr.insert(-1, 'bak') new_path = '.'.join(arr) print(image_path, new_path) shutil.copyfile(image_path, new_path)
def compress_image(image_path): source = tinify.from_file(image_path) source.to_file(image_path)
def main(): image_path = sys.argv[1] bak_image(image_path) add_mark(image_path) compress_image(image_path)
if __name__ == '__main__': main()
|