LoginSignup
28
31

More than 5 years have passed since last update.

pythonによる画像処理(Pillow)

Posted at

動機

・raspberry piとセンサを組み合わせて遊ぶために、pythonで画像処理の勉強を行った

使用する環境

Anacondaをインストール(python3.5)
https://www.continuum.io/downloads

sample.png

・ピクセルに格納される情報はすべて一次元
・一般に次式でRGBからグレースケールに変換

Y = 0.299 * R + 0.587 * G + 0.114 * B

・Yは輝度(光の強さを表す)

グレースケールの変換コード
test.py
#画像を表現するクラスをロード
from PIL import Image
#画像をsampleを開く
img = Image.open('sample.png')

# グレースケールに変換
gray_img = img.convert('L')
gray_img.save('sample-gray.png')

以下のコマンドで実行

$ python test.py

sample-gray.png

RGBの入れ替え

test2.py
from PIL import Image
img = Image.open('sample.png')

r, g, b = img.split()

img = Image.merge("RGB", (b,r,g))
img.save('sample-convert.png')

sample-convert.png

最後に

画像に対して加工することができたため、次回は画像認識(スケール変化にも対応)をしてみたい

28
31
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
28
31