Files
SillyTavern_replica/clear_default_avatar.py
2026-05-01 15:44:14 +08:00

54 lines
1.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
清空默认头像图片中的嵌入JSON数据
"""
from PIL import Image
import io
def clear_png_text_data(png_path):
"""
清空PNG文件中的tEXt文本数据包括嵌入的JSON
Args:
png_path: PNG文件路径
"""
try:
# 打开图片
img = Image.open(png_path)
print(f"原始PNG文本数据键: {list(img.text.keys()) if img.text else ''}")
# 创建一个新的图片对象,复制像素数据但不复制文本数据
if img.mode == 'RGBA':
new_img = Image.new('RGBA', img.size)
else:
new_img = Image.new('RGB', img.size)
# 复制像素数据
new_img.paste(img)
# 确保新图片没有文本数据
new_img.text = {}
# 保存回原文件
buffer = io.BytesIO()
new_img.save(buffer, format='PNG')
buffer.seek(0)
with open(png_path, 'wb') as f:
f.write(buffer.read())
# 验证是否已清空
verify_img = Image.open(png_path)
print(f"清空后PNG文本数据键: {list(verify_img.text.keys()) if verify_img.text else ''}")
print(f"✓ 成功清空 {png_path} 中的JSON数据")
except Exception as e:
print(f"✗ 处理失败: {e}")
import traceback
traceback.print_exc()
if __name__ == '__main__':
png_file = 'data/characters/defult.png'
print(f"正在处理: {png_file}")
clear_png_text_data(png_file)