fix
This commit is contained in:
58
scpt/ai_svg_package/fontawesome.py
Normal file
58
scpt/ai_svg_package/fontawesome.py
Normal file
@ -0,0 +1,58 @@
|
||||
import os
|
||||
import requests
|
||||
from bs4 import BeautifulSoup
|
||||
|
||||
# ▼ ChatGPTに渡す仮のSVG生成(今は適当なSVGで代用)
|
||||
def generate_svg_from_name(name):
|
||||
return f"""
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64">
|
||||
<text x="0" y="55" font-size="60">{name[0].upper()}</text>
|
||||
</svg>
|
||||
""".strip()
|
||||
|
||||
# ▼ 1. Font Awesomeの名前一覧をスクレイピング(例:Font Awesome 6 Free icons)
|
||||
def fetch_fontawesome_names(url="https://fontawesome.com/icons"):
|
||||
response = requests.get(url)
|
||||
soup = BeautifulSoup(response.text, 'html.parser')
|
||||
icons = soup.select('[class^="fa-"]')
|
||||
names = set()
|
||||
for tag in icons:
|
||||
classes = tag.get("class", [])
|
||||
for cls in classes:
|
||||
if cls.startswith("fa-") and not cls.startswith("fa-fw"):
|
||||
names.add(cls.replace("fa-", ""))
|
||||
return sorted(names)
|
||||
|
||||
# ▼ 2. SVGファイル保存
|
||||
def save_svgs(icon_names, output_dir="svgs"):
|
||||
os.makedirs(output_dir, exist_ok=True)
|
||||
for name in icon_names:
|
||||
svg = generate_svg_from_name(name)
|
||||
with open(f"{output_dir}/{name}.svg", "w", encoding="utf-8") as f:
|
||||
f.write(svg)
|
||||
|
||||
# ▼ 3. CSS生成
|
||||
def generate_css(icon_names, output_file="icons.css"):
|
||||
lines = [":root {"]
|
||||
for name in icon_names:
|
||||
lines.append(f""" --icon-{name}: url("svgs/{name}.svg");""")
|
||||
lines.append("}\n")
|
||||
lines.append(".icon { width: 1em; height: 1em; display: inline-block; background-size: contain; background-repeat: no-repeat; }\n")
|
||||
for name in icon_names:
|
||||
lines.append(f""".icon-{name} {{ background-image: var(--icon-{name}); }}""")
|
||||
with open(output_file, "w", encoding="utf-8") as f:
|
||||
f.write("\n".join(lines))
|
||||
|
||||
# ▼ 全自動実行
|
||||
if __name__ == "__main__":
|
||||
print("Fetching font names...")
|
||||
icon_names = fetch_fontawesome_names()
|
||||
print(f"Found {len(icon_names)} icons")
|
||||
|
||||
print("Generating SVGs...")
|
||||
save_svgs(icon_names[:10]) # ※テストとして10件のみ処理(全件だと数百個以上)
|
||||
|
||||
print("Generating CSS...")
|
||||
generate_css(icon_names[:10])
|
||||
|
||||
print("Complete!")
|
54
scpt/ai_svg_package/svg-icon-generator.py
Normal file
54
scpt/ai_svg_package/svg-icon-generator.py
Normal file
@ -0,0 +1,54 @@
|
||||
import os
|
||||
import openai
|
||||
|
||||
# ====================
|
||||
# 設定
|
||||
# ====================
|
||||
openai.api_key = os.getenv("OPENAI_API_KEY") # または直接キーをここに書く
|
||||
|
||||
# 保存先ディレクトリ
|
||||
SAVE_DIR = "generated_svgs"
|
||||
os.makedirs(SAVE_DIR, exist_ok=True)
|
||||
|
||||
# 生成したいアイコン名リスト
|
||||
icon_names = ["home", "star", "chat", "user"]
|
||||
|
||||
# ====================
|
||||
# ChatGPTを使ってSVGコードを生成
|
||||
# ====================
|
||||
def generate_svg_code(icon_name):
|
||||
prompt = f"""
|
||||
SVGアイコンを作ってください。テーマは「{icon_name}」です。
|
||||
SVGのコードだけを出力してください。サイズは24x24pxで、シンプルでCSSで色指定可能にしてください。
|
||||
"""
|
||||
|
||||
response = openai.ChatCompletion.create(
|
||||
model="gpt-4",
|
||||
messages=[
|
||||
{"role": "system", "content": "あなたはSVGアイコンを生成するアシスタントです。"},
|
||||
{"role": "user", "content": prompt}
|
||||
]
|
||||
)
|
||||
|
||||
svg_code = response.choices[0].message.content.strip()
|
||||
return svg_code
|
||||
|
||||
# ====================
|
||||
# SVGファイルを保存
|
||||
# ====================
|
||||
def save_svg(icon_name, svg_code):
|
||||
filename = os.path.join(SAVE_DIR, f"{icon_name}.svg")
|
||||
with open(filename, "w", encoding="utf-8") as f:
|
||||
f.write(svg_code)
|
||||
print(f"✅ Saved: {filename}")
|
||||
|
||||
# ====================
|
||||
# メイン処理
|
||||
# ====================
|
||||
if __name__ == "__main__":
|
||||
for name in icon_names:
|
||||
try:
|
||||
svg = generate_svg_code(name)
|
||||
save_svg(name, svg)
|
||||
except Exception as e:
|
||||
print(f"❌ Error generating {name}: {e}")
|
Reference in New Issue
Block a user