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
| import datetime import os import requests import shutil import sys import urllib.parse from PIL import Image, ImageDraw, ImageFont
TEMP_PATH = "C:\\Users\\BIGFISH\\AppData\\Local\\Temp\\Typora\\"
print("Upload Success:")
def add_watermark(input_image_path, output_image_path): watermark_text = "mbigfish.com" original = Image.open(input_image_path)
draw = ImageDraw.Draw(original)
font = ImageFont.truetype("arial.ttf", 36)
width, height = original.size
text_bbox = draw.textbbox((0, 0), watermark_text, font=font) text_width = text_bbox[2] - text_bbox[0] text_height = text_bbox[3] - text_bbox[1]
x = width - text_width - 10 y = height - text_height - 10
draw.text((x, y), watermark_text, font=font, fill=(238, 130, 238, 120))
original.save(output_image_path)
for old_image_url in sys.argv[1:]: old_image_urlparse = urllib.parse.urlparse(old_image_url) time_name = datetime.datetime.now().strftime('%Y%m%d%H%M%S%f') new_image_name = f"image-{time_name}" new_image_suffix = os.path.splitext(old_image_urlparse.path)[-1] new_image_url = f"{TEMP_PATH}{new_image_name}{new_image_suffix}"
watermark_image_url = f"watermark-{time_name}{new_image_suffix}" add_watermark(old_image_url, watermark_image_url) if old_image_urlparse.scheme in ["http", "https"]: with open(new_image_url, "wb") as image: image.write(requests.get(watermark_image_url).content) else: shutil.copy(watermark_image_url, new_image_url) os.remove(watermark_image_url) print(os.popen(f"hau upload -f {new_image_url}").readlines()[1].strip("\n"))
|