Files
nanocode/it_automation_test/scripts/create_test_image.py
T
Ubuntu 32b43f85bd a
2026-07-03 16:58:28 +08:00

94 lines
2.9 KiB
Python

#!/usr/bin/env python3
"""
创建测试图片用于小红书发布
"""
from PIL import Image, ImageDraw, ImageFont
import os
def create_test_image(text="测试图片", output_path="test_image.png"):
"""创建简单的测试图片"""
# 图片尺寸(小红书推荐比例)
width, height = 1080, 1440 # 3:4比例
# 创建图片
image = Image.new('RGB', (width, height), color=(70, 130, 180)) # 钢蓝色背景
draw = ImageDraw.Draw(image)
try:
# 尝试加载字体
font = ImageFont.truetype("/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc", 60)
except:
# 回退到默认字体
font = ImageFont.load_default()
# 添加文字
text_width = draw.textlength(text, font=font)
text_x = (width - text_width) // 2
text_y = height // 2 - 30
# 文字阴影效果
draw.text((text_x+2, text_y+2), text, fill=(0, 0, 0), font=font)
draw.text((text_x, text_y), text, fill=(255, 255, 255), font=font)
# 添加副标题
subtitle = "nanobot新仓库代码测试"
try:
sub_font = ImageFont.truetype("/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc", 40)
except:
sub_font = font
sub_width = draw.textlength(subtitle, font=sub_font)
sub_x = (width - sub_width) // 2
sub_y = text_y + 80
draw.text((sub_x, sub_y), subtitle, fill=(255, 255, 200), font=sub_font)
# 保存图片
image.save(output_path, 'PNG')
print(f"✅ 测试图片创建成功: {output_path}")
print(f"📏 图片尺寸: {width}x{height}")
return output_path
def main():
"""主函数"""
print("🖼️ 创建测试图片用于小红书发布")
# 创建图片目录
image_dir = "/home/ubuntu/nanocode/it_automation_test/images"
os.makedirs(image_dir, exist_ok=True)
# 创建测试图片
image_path = os.path.join(image_dir, "test_post_image.png")
create_test_image("小红书发布测试", image_path)
# 验证图片
if os.path.exists(image_path):
size = os.path.getsize(image_path)
print(f"📁 图片路径: {image_path}")
print(f"📦 文件大小: {size:,} 字节")
# 显示图片信息
try:
with Image.open(image_path) as img:
print(f"🎨 图片格式: {img.format}")
print(f"📐 图片尺寸: {img.size}")
print(f"🌈 图片模式: {img.mode}")
except Exception as e:
print(f"⚠️ 无法读取图片信息: {e}")
return image_path
else:
print("❌ 图片创建失败")
return None
if __name__ == "__main__":
image_path = main()
if image_path:
print(f"\n🎉 测试图片准备完成,可以用于小红书发布")
print(f"💡 使用命令: xhs post --title '标题' --body '内容' --images '{image_path}'")
else:
print("\n❌ 测试图片创建失败")
exit(1)