LoginSignup
3
7

More than 5 years have passed since last update.

python pillowで画像のトリミング自動化

Posted at

TL;DR

画像から右端の凡例をそぎ落とす

できあがりサイズ調査

この辺が要らないからここまで削りたい、を今回はPhotoshopのカーソル位置表示で座標確認した。

必要ライブラリ

pillow

※Anacondaに入っている

実装

crop.py
import glob
from PIL import Image, ImageFilter

# 対象フォルダ配下の、対象拡張子ファイル一覧のリストを返す
def ls_suffix(search_dir, suffix):
  file_name_list = glob.glob(search_dir + "*." + suffix)
  return file_name_list

# 幅をafter_widthに変更して上書き保存。高さそのまま。
# 基準点を左上原点にしているため、右端を削ぐ
def crop_right(picture_path, after_width):
  im = Image.open(picture_path)
  before_width,before_height = im.size

  im = im.crop((0,0,after_width,before_height))
  im.save(picture_path, quality=95)


if __name__ == "__main__":
  file_name_list = ls_suffix("./", "png")

  for file_path in file_name_list:
    crop_right(file_path, 600)

Before, After

※Webページだと適宜拡大されたりしてて、印象ちょっと違うかもですが、今回削ったのは凡例だけ。

before

doshamesh_201803220810.png

after

doshamesh_201803220810.png

参考文献

http://www.yukun.info/blog/2008/08/python-directory-listdir-glob.html
https://note.nkmk.me/python-pillow-image-crop-trimming/

3
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
7