from PIL import Image
import sys
import os

def pad_to_square(image_path, output_path, size):
    img = Image.open(image_path).convert("RGBA")
    w, h = img.size
    scale = size / w
    new_h = int(h * scale)
    img = img.resize((size, new_h), Image.LANCZOS)
    canvas = Image.new("RGBA", (size, size), (0, 0, 0, 0))
    top = (size - new_h) // 2
    canvas.paste(img, (0, top), img)
    canvas.save(output_path)

if __name__ == "__main__":
    if len(sys.argv) != 4:
        print("使い方: python pad_to_square.py 入力画像 出力画像 サイズ")
        sys.exit(1)
    in_path = sys.argv[1]
    out_path = sys.argv[2]
    size = int(sys.argv[3])
    if not os.path.isfile(in_path):
        print("入力画像が存在しません")
        sys.exit(1)
    pad_to_square(in_path, out_path, size)
    print(f"{out_path} に保存しました")