Skip to content

Instantly share code, notes, and snippets.

@kotoripiyopiyo
Last active April 17, 2021 13:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kotoripiyopiyo/aafae0b5cf023a7fd565214f428db4f6 to your computer and use it in GitHub Desktop.
Save kotoripiyopiyo/aafae0b5cf023a7fd565214f428db4f6 to your computer and use it in GitHub Desktop.
退屈なことはPythonにやらせようChapter17を例題見ずに書いた
#!/usr/bin/env python3
# 画像をリサイズしてロゴを貼り付ける
import os
from PIL import Image
import glob
dirname = './chapter17/'
os.makedirs('changed', exist_ok=True)
new_dirname = './changed'
SQUARE_FIT_SIZE = 300
LOGO_FILENAME = 'catlogo.png'
# ロゴを取得
logo_img = Image.open(os.path.join(dirname, LOGO_FILENAME))
logo_width, logo_height = logo_img.size
# todo 画像をサイズ変更する
def img_resize(base_img, new_width):
width, height = base_img.size
height = round(height * new_width/width)
return base_img.resize((new_width, height))
# todo 指定ディレクトリの全画像をループする
img_list = glob.glob(dirname + '*.JPG')
new_img_width = 400
i = 0
for img in img_list:
orizinal_img = Image.open(img)
i += 1
print(f'{i:02}番目「{img}」の画像をリサイズ中。')
new_img = img_resize(orizinal_img, new_img_width) # リサイズ
new_img_width, new_img_height = new_img.size
print(f'{i:02}番目「{img}」の画像にロゴを追加。')
new_img.paste(logo_img, (new_img_width - logo_width, new_img_height - logo_height), logo_img) # ロゴを追加
print(f'{i:02}番目「{img}」の画像を保存。')
filename = f'{i:02}'
new_img.save(os.path.join(new_dirname, filename+'.jpg')) # 保存
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment