LoginSignup
2
2

More than 5 years have passed since last update.

iPython上でPillowを使ってみる(その2)

Last updated at Posted at 2015-03-21

Pillowの作業フロー(その2)

  1. Image.open で画像を読み込む
  2. 画像をゴニョゴニョする
  3. Image.save で編集した画像を保存する

では行ってみましょー。

%pylab inline
from PIL import Image,ImageDraw,ImageFont

ファイルのフォーマット変換

# 画像を読み込み
img = Image.open('in-image/lena_std.tif')

# ファイルの情報表示
print('size    : ', img.size)
print('format  : ', img.format) 
print('mode    : ', img.mode) 
print('palette : ', img.palette) 
print('info    : ', img.info) 

pl_img = np.array(img) ; plt.imshow( pl_img ) # 表示

# ファイル形式を変換して保存(拡張子を見て自動的に判定する)
img.save('work-image/lena.jpg') 

lena.jpg

画像ファイルのサイズ変更(resize)

img = Image.open('work-image/lena.jpg')

# ファイルの情報表示
print('size    : ', img.size)
print('format  : ', img.format) 
print('mode    : ', img.mode) 
print('palette : ', img.palette) 
print('info    : ', img.info) 

# 200*200にサイズ変更
resize_img = img.resize((200,200),Image.ANTIALIAS)

# ファイルの情報表示
print('size    : ', resize_img.size)
print('format  : ', resize_img.format) 
print('mode    : ', resize_img.mode) 
print('palette : ', resize_img.palette) 
print('info    : ', resize_img.info) 

# 変換したデータを保存
resize_img.save('work-image/resize_lena.jpg') 

pl_img = np.array(resize_img) ; plt.imshow( pl_img ) # 表示

resize_lena.jpg

画像のトリミング

# 元データ(img)はすでに読みこんであるので、それをトリミング
trim_img = img.crop((64,64,448,448))

pl_img = np.array(trim_img) ; plt.imshow( pl_img ) # 表示

# トリミングしたデータを保存
trim_img.save('work-image/trim_lena.jpg') 

trim_lena.jpg

↓nbviewerにノートブックをあげてみました(っていうか、こっちがメイン)
nbviewer.ipython.org/github/suto3/git-public/blob/master/python/notebook/Pillow-workflow02.ipynb

↓作業環境については、こちら
Pillow環境構築 -- virtualenvによる仮想環境、iPythonによるインタラクティブ環境 - Qiita

iPython上でPillowを使ってみる(その1) - Qiita

iPython上でPillowを使ってみる(その3) - Qiita

いや~、iPythonは楽だw。

2
2
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
2
2