python pillow 色々なメソッド
はじめに
今回はpillowを使って線や四角形などを描画してみる。
準備
下記のプログラムを用意する。
- pil.py
from PIL import Image, ImageDraw #640x480の黒い画像を準備 im = Image.new('RGB', (640, 480), (0, 0, 0)) draw = ImageDraw.Draw(im) #丸四角の線の色 outLineColor = (255, 255, 255) #丸四角の座標値設定 #((左上のx座標, 左上のy座標), (右下のx座標, 右下のy座標)) #(左上のx座標, 左上のy座標, 右下のx座標, 右下のy座標) #丸の描画 draw.ellipse((50, 50, 150, 150), fill=(255, 0, 0), outline=outLineColor) #四角の描画 draw.rectangle((200, 100, 300, 200), fill=(0, 255, 0), outline=outLineColor) #線の座標値設定 #(x1, y1, x2, y2, x3, y3...) #((x1, y1), (x2, y2), (x3, y3)...) #線の描画 #draw.line(((350, 100), (450, 250), (500, 100), (350, 200), (350, 100)), fill=(255, 255, 0), width=3) linePoints = ( (350, 100), (450, 250), (500, 100), (350, 200), (350, 100) ) draw.line(linePoints, fill=(255, 255, 0), width=3) #多角形の描画 polygonPoints = ( (550, 200), (450, 350), (500, 200), (550, 300), (250, 200) ) draw.polygon(polygonPoints, fill=(0, 0, 255),outline=outLineColor) #点の描画 points = ( (250, 400), (250, 450), (300, 450), (350, 400), (400, 400) ) draw.point(points, fill=(0, 255, 255)) #保存しておく im.save('image.png') #めんどくさいから表示させる im.show()
実行方法
python pil.py
- 実行結果
ごちゃごちゃしているがちゃんと表示された
pointは点なので分かりづらいがちゃんと表示されている
まとめ
Pillowのほうが直感的に使いやす印象がある。
グレー変換とかも挑戦してみようと思う。