from PIL import Image import os # 入力フォルダ input_path = "./genFloor" # 出力フォルダ output_path = "./tmp1" # 入力フォルダからファイル名を取得 all_files = os.listdir(input_path) #特定の拡張子のファイルのみを取り出す files = [i for i in all_files if i.endswith('.jpg') == True] for file_name in files: input = Image.open(input_path + '/' + file_name) # ファイル名抽出 file_name_1 = file_name.split('.')[0] for i in range(3): # 画像を切り抜く v = i*120 for k in range(4): h = k*120 output = input.crop((h, v, h+120, v+120)) # (left, upper, right, lower) file_name_2 = str(i*4+k).zfill(2) output.save(output_path + '/' + file_name_1 + '_' + file_name_2 + '.jpg')