huhan3000/tools/ai-tools/scripts/image_analyzer.py

80 lines
2.4 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.

#!/usr/bin/env python3
"""
图片分析工具 - 使用月之暗面 API 分析图片内容
"""
import base64
import os
from openai import OpenAI
def encode_image(image_path):
"""将图片编码为 base64"""
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
def analyze_image(image_path, prompt="请详细描述这张图片的内容"):
"""分析图片内容"""
# 月之暗面配置
client = OpenAI(
api_key="sk-lEk0pAIZ1EDgUkflq2is5uT2VbhuoKGpO5sNOSnuuccsD68r",
base_url="https://api.moonshot.cn/v1"
)
# 编码图片
base64_image = encode_image(image_path)
try:
response = client.chat.completions.create(
model="moonshot-v1-8k",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": prompt},
{
"type": "image_url",
"image_url": {
"url": f"data:image/png;base64,{base64_image}"
}
}
]
}
],
max_tokens=1000
)
return response.choices[0].message.content
except Exception as e:
return f"分析失败: {e}"
def batch_analyze_images(image_dir, output_file="image_analysis.md"):
"""批量分析图片"""
results = []
# 获取所有 PNG 图片
png_files = [f for f in os.listdir(image_dir) if f.endswith('.png')]
png_files.sort() # 按文件名排序
for filename in png_files:
image_path = os.path.join(image_dir, filename)
print(f"正在分析: {filename}")
analysis = analyze_image(
image_path,
"请详细描述这张图片的内容,包括文字、图表、人物、建筑等所有可见元素"
)
results.append(f"## {filename}\n\n{analysis}\n\n---\n")
# 保存结果
with open(output_file, 'w', encoding='utf-8') as f:
f.write("# 图片分析结果\n\n")
f.writelines(results)
print(f"分析完成,结果保存到: {output_file}")
if __name__ == "__main__":
# 示例:分析序章的图片
image_dir = "images/0 序令人又敬又畏的_忽里勒台_大会"
batch_analyze_images(image_dir, "序章图片分析.md")