Bye Bye Moore

PoCソルジャーな零細事業主が作業メモを残すブログ

python3でも使える画像操作ライブラリPillow

Pillowはpython3でも使える画像操作ライブラリです。

参考もと

サムネイル

from __future__ import print_function
import os, sys
from PIL import Image

size = (128, 128)

for infile in sys.argv[1:]:
    outfile = os.path.splitext(infile)[0] + ".thumbnail"
    if infile != outfile:
        try:
            im = Image.open(infile)
            im.thumbnail(size)
            im.save(outfile, "JPEG")
        except IOError:
            print("cannot create thumbnail for", infile)

使い方は

$ python make_thumbnail.py image.jpeg
$ ls 
make_thumbnail.py
image.jpeg
image.jpeg.thumbnail

コントラストを弄る

黒埋めする。

out = im.point(lambda i: i * 0)
out.save(outfile, "JPEG")