目次
機械学習とは
プログラミング無しで機械学習
機械学習プログラミング入門
初めてのKeras2.0
- KerasでFineTuning(作成中)
- pythonメモ(作成中)
機械学習で画像を切り抜きたいときがある。そんなときは、以下のようにPIL(Pillow)を用いると便利。Jupyter Notebookで可視化するためにmatplotlibとnumpyも利用する。
Windows8.1
Python 3.6.1
Anaconda 4.4.0 (64-bit)
Tensorflow 1.2.1
Keras 2.0.6
PIL(pillow)は、あらかじめ、Anacondaに入っているが、他の環境で、import PILでエラーが出る場合は、コマンドプロンプトで、
pip install pillow
でPILをインストールすることができる。
こちら(外部リンク)から、1280×856の画像をダウンロードして、ipynbと同じフォルダに、greece.jpgという名前で保存。
この画像の右上の部分だけを切り抜いて、jupyter notebook上に表示しつつ、切り抜いた画像をフォルダにも新たに保存したい!
jupyter notebookで以下のコードをコピペして、Shift+Enter
# coding:utf-8
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
# Jupyterでインライン表示するための宣言
%matplotlib inline
# 画像の読み込み
img = Image.open("./greece.jpg")
# 画像をarrayに変換
im_list = np.asarray(img)
#貼り付け
plt.imshow(im_list)
#表示
print("./greece .jpg")
print(img.size)
plt.show()
# 画像の切り抜き(PIL)
width = img.size[0]
height = img.size[1]
img2 = img.crop(
(
width -560,
0,
width - 50,
height - 300
)
)
img2.save("img2.jpg")
#画像をarrayに変換
im_list = np.asarray(img2)
#貼り付け
plt.imshow(im_list)
#表示
print("img2.jpg")
print(img2.size)
plt.show()
これにより、元画像greece.jpgの右上の部分の切り抜き画像が、greece.jpgと同じフォルダに、img2.jpgという名前で保存される。また、その結果も、jupyter notebookで見ながら、切り抜き位置を簡単に調整できる。
http://qiita.com/ukwksk/items/483d1b9e525667b77187
python3系でのPython Image Libraryの使用方法
http://www.mwsoft.jp/programming/computer_vision_with_python/1_1_pil.html
Pillow(Python Imaging Library)のインストールと簡単なサンプルコード
http://python-remrin.hatenadiary.jp/entry/2017/05/20/181211
20170520 PILの使い方(1)
http://matthiaseisen.com/pp/patterns/p0202/
Crop Images with PIL/Pillow
画像の切り抜きは、crop
thumbnailは、破壊的、元画像がなくなるので、あらかじめコピーを作っておいたほうが良い
resize
http://qiita.com/zaburo/items/5637b424c655b136527a
Matplotlibで画像を表示
zaburo
2015年12月27日に投稿