初始化图像水印CLI工具
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
import os
|
||||
import sys
|
||||
from watermark import watermark_image, increment_count, load_font
|
||||
from config import OUTPUT_DIR, SUPPORTED_INPUT_EXTENSIONS, WORK_DIR
|
||||
|
||||
|
||||
def print_help():
|
||||
help_text = """
|
||||
用法:
|
||||
"图片路径" "水印内容" 加水印,保持原格式输出
|
||||
"图片路径>输出格式" "水印内容" 加水印并转换格式(格式可选:jpg/png/bmp/gif/tiff/webp)
|
||||
/exit 退出程序
|
||||
/get 查询已处理图片总数
|
||||
|
||||
示例:
|
||||
photo.jpg "版权所有"
|
||||
photo.jpg>PNG "2026"
|
||||
"""
|
||||
print(help_text.strip())
|
||||
|
||||
|
||||
def print_stats():
|
||||
count = 0
|
||||
if os.path.exists(os.path.join(WORK_DIR, ".processed_count")):
|
||||
with open(os.path.join(WORK_DIR, ".processed_count"), "r", encoding="utf-8") as f:
|
||||
try:
|
||||
count = int(f.read().strip())
|
||||
except ValueError:
|
||||
pass
|
||||
print(f"当前已处理图片数量: {count}")
|
||||
|
||||
|
||||
def process_single(image_arg, text):
|
||||
if ">" not in image_arg:
|
||||
# 无格式转换,保持原格式
|
||||
output_ext = None
|
||||
else:
|
||||
parts = image_arg.rsplit(">", 1)
|
||||
image_arg, out_ext = parts
|
||||
out_ext = out_ext.strip().lstrip(".")
|
||||
if out_ext.lower() not in SUPPORTED_INPUT_EXTENSIONS:
|
||||
print(f"不支持的输出格式: .{out_ext},支持的格式: {', '.join(sorted(SUPPORTED_INPUT_EXTENSIONS))}")
|
||||
return
|
||||
output_ext = f".{out_ext.lower()}"
|
||||
|
||||
input_path = image_arg.strip()
|
||||
if not os.path.isfile(input_path):
|
||||
print(f"找不到输入图片: {input_path}")
|
||||
return
|
||||
|
||||
_, ext = os.path.splitext(input_path)
|
||||
if ext.lower() not in SUPPORTED_INPUT_EXTENSIONS:
|
||||
print(f"不支持的输入格式: {ext},支持的格式: {', '.join(sorted(SUPPORTED_INPUT_EXTENSIONS))}")
|
||||
return
|
||||
|
||||
output_path = watermark_image(input_path, text, output_ext=output_ext)
|
||||
total = increment_count()
|
||||
print(f"✅ 已保存: {output_path}")
|
||||
print(f"📊 累计处理图片: {total} 张")
|
||||
|
||||
|
||||
def main():
|
||||
if len(sys.argv) >= 3:
|
||||
# 非交互模式:直接处理一张图片
|
||||
process_single(sys.argv[1], sys.argv[2])
|
||||
return
|
||||
|
||||
print("========================================")
|
||||
print(" 图像水印工具 v1.0")
|
||||
print(" 输入 /help 查看帮助")
|
||||
print("========================================")
|
||||
while True:
|
||||
try:
|
||||
cmd = input("\n> ").strip()
|
||||
except (EOFError, KeyboardInterrupt):
|
||||
print("\n再见!")
|
||||
break
|
||||
|
||||
if not cmd:
|
||||
continue
|
||||
|
||||
if cmd == "/exit" or cmd == "exit":
|
||||
print("再见!")
|
||||
break
|
||||
elif cmd == "/get":
|
||||
print_stats()
|
||||
elif cmd == "/help":
|
||||
print_help()
|
||||
elif ">" in cmd:
|
||||
# 格式转换模式: "图片>格式 水印内容"
|
||||
parts = cmd.split(">", 1)
|
||||
image_part = parts[0].strip()
|
||||
rest = parts[1].strip()
|
||||
if not rest:
|
||||
print("缺少水印内容,请重试。输入 /help 查看帮助。")
|
||||
continue
|
||||
# 找出 ">格式 " 后面的水印文本(用 ">格式 " 分割后取第二部分)
|
||||
if " " in rest:
|
||||
final_format, text = rest.split(" ", 1)
|
||||
full_arg = f"{image_part}>{final_format}"
|
||||
else:
|
||||
print("格式参数后缺少水印内容,请重试。输入 /help 查看帮助。")
|
||||
continue
|
||||
process_single(full_arg, text)
|
||||
else:
|
||||
# 普通模式: "图片路径 水印内容"
|
||||
parts = cmd.split(None, 1)
|
||||
if len(parts) < 2:
|
||||
print("格式错误,需要提供图片路径和水印内容。输入 /help 查看帮助。")
|
||||
continue
|
||||
image_arg, text = parts
|
||||
process_single(image_arg, text)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user