import os
from PIL import Image
# --- PC環境に合わせた直接指定 ---
# OneDrive上のデスクトップパスを確実に指定します
desktop_path = r"C:\Users\bluebee\OneDrive\デスクトップ"
# 元画像フォルダと出力先フォルダ
input_dir = os.path.join(desktop_path, "original_photos")
output_dir = os.path.join(desktop_path, "web_ready")
# サイズ設定
THUMB_SIZE = (300, 300) # サムネイル
FULL_SIZE = (1200, 1200) # モーダル用
# フォルダ作成
if not os.path.exists(output_dir):
os.makedirs(output_dir)
def process_images():
count = 0
# フォルダ内のファイルをスキャン
for filename in os.listdir(input_dir):
if filename.lower().endswith(('.jpg', '.jpeg', '.png')):
img_path = os.path.join(input_dir, filename)
base_name = os.path.splitext(filename)[0]
try:
with Image.open(img_path) as img:
# 1. Full画像の生成
img_full = img.copy()
img_full.thumbnail(FULL_SIZE, Image.Resampling.LANCZOS)
img_full.save(os.path.join(output_dir, f"{base_name}-full.jpg"), "JPEG", quality=85)
# 2. Thumbnail画像の生成
img_thumb = img.copy()
img_thumb.thumbnail(THUMB_SIZE, Image.Resampling.LANCZOS)
img_thumb.save(os.path.join(output_dir, f"{base_name}-thumb.jpg"), "JPEG", quality=80)
print(f"成功: {filename} を変換しました")
count += 1
except Exception as e:
print(f"エラー: {filename} の処理に失敗しました - {e}")
return count
if __name__ == "__main__":
print(f"--- 画像変換開始 ---")
if os.path.exists(input_dir):
processed_count = process_images()
print(f"\n完了! {processed_count}枚の画像を処理しました。")
print(f"保存先: {output_dir}")
else:
print(f"【確認】デスクトップに 'original_photos' という名前のフォルダが見当たりません。")
print(f"探した場所: {input_dir}")
Microsoft Windows [Version 10.0.26200.8117]
(c) Microsoft Corporation. All rights reserved.
C:\Users\bluebee>python "C:\Users\bluebee\OneDrive\デスクトップ\convert_images.py
--- 画像変換開始 ---
成功: P3272327.jpg を変換しました
成功: P3272332.jpg を変換しました
成功: P3272338.jpg を変換しました
成功: P3272341.jpg を変換しました
成功: P3272342.jpg を変換しました
成功: P3272344.jpg を変換しました
成功: P3272345.jpg を変換しました
成功: P3272346.jpg を変換しました
成功: P3272348.jpg を変換しました
成功: P3272350.jpg を変換しました
成功: P3272351.jpg を変換しました
成功: P3272353.jpg を変換しました
成功: P3272354.jpg を変換しました
成功: P3272356.jpg を変換しました
成功: P3272357.jpg を変換しました
成功: P3272358.jpg を変換しました
成功: P3272364.jpg を変換しました
成功: P3272368.jpg を変換しました
成功: P3272369.jpg を変換しました
成功: P3272370.jpg を変換しました
成功: P3272372.jpg を変換しました
成功: P3272373.jpg を変換しました
成功: P3272375.jpg を変換しました
成功: P3272376.jpg を変換しました
完了! 24枚の画像を処理しました。
保存先: C:\Users\bluebee\OneDrive\デスクトップ\web_ready
C:\Users\bluebee>