129 lines
4.4 KiB
Python
129 lines
4.4 KiB
Python
import os
|
||
import sys
|
||
from watermark import watermark_image, increment_count
|
||
from config import OUTPUT_DIR, SUPPORTED_INPUT_EXTENSIONS, SUPPORTED_OUTPUT_EXTENSIONS, WORK_DIR
|
||
|
||
|
||
def print_help():
|
||
help_text = """
|
||
========================================
|
||
imgwatermark - 图像水印工具 v1.0
|
||
支持: .jpg .png .bmp .gif .tiff .webp
|
||
输出: jpg / png / bmp / gif / tiff / webp
|
||
========================================
|
||
|
||
命令:
|
||
<图片路径> \"水印内容\" 加水印,保持原格式输出
|
||
<图片路径>>.png \"内容\" 加水印并转换格式(格式可选:jpg/png/bmp/gif/tiff/webp)
|
||
/exit 或 exit 退出程序
|
||
/get 或 get 显示已处理图片统计
|
||
/help 或 help 显示本帮助信息
|
||
|
||
示例:
|
||
D:\\img\\photo.jpg \"版权所有\"
|
||
D:\\img\\photo.jpg>.png \"2026\"
|
||
/get
|
||
"""
|
||
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):
|
||
"""处理单张图片:可选格式转换,添加水印,计数+1。"""
|
||
# 去掉首尾引号(兼容命令行传入带引号的路径)
|
||
image_arg = image_arg.strip().strip('"').strip("'")
|
||
text = text.strip().strip('"').strip("'")
|
||
|
||
if ">" not in image_arg:
|
||
output_ext = None
|
||
else:
|
||
parts = image_arg.rsplit(">", 1)
|
||
image_arg, out_ext = parts[0].strip(), parts[1].strip().lstrip(".")
|
||
if out_ext.lower() not in SUPPORTED_INPUT_EXTENSIONS:
|
||
print(f"不支持的输出格式: .{out_ext},支持的格式: {', '.join(sorted(SUPPORTED_OUTPUT_EXTENSIONS))}")
|
||
return
|
||
output_ext = f".{out_ext.lower()}"
|
||
|
||
input_path = image_arg
|
||
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("=" * 40)
|
||
print(" imgwatermark - 图像水印工具 v1.0")
|
||
print(" 输入 /help 查看帮助")
|
||
print("=" * 40)
|
||
while True:
|
||
try:
|
||
cmd = input("\n> ").strip()
|
||
except (EOFError, KeyboardInterrupt):
|
||
print("\n再见!")
|
||
break
|
||
|
||
if not cmd:
|
||
continue
|
||
|
||
# 处理命令前缀(支持带/或不带/)
|
||
if cmd in ("/exit", "exit"):
|
||
print("再见!")
|
||
break
|
||
elif cmd in ("/get", "get"):
|
||
print_stats()
|
||
elif cmd in ("/help", "help"):
|
||
print_help()
|
||
elif ">" in cmd:
|
||
# 格式转换模式: "图片路径>.png 水印内容" 或 "图片路径>PNG 水印内容"
|
||
parts = cmd.split(">", 1)
|
||
image_part = parts[0].strip()
|
||
rest = parts[1].strip()
|
||
if not rest:
|
||
print("缺少水印内容,请重试。输入 /help 查看帮助。")
|
||
continue
|
||
# rest 中第一个空格前是格式,后面是水印文本
|
||
tokens = rest.split(None, 1)
|
||
if len(tokens) < 2:
|
||
print("格式参数后缺少水印内容,请重试。输入 /help 查看帮助。")
|
||
continue
|
||
final_format, text = tokens
|
||
full_arg = f"{image_part}>{final_format}"
|
||
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()
|