2020年5月19日火曜日

【Pillow】ImageDrawモジュールによる直線、四角、円、多角形描画

 画像処理ライブラリPillowのImageDrawモジュールを用いると図形が描画できる。
直線:ImageDraw.line
四角形:ImageDraw.rectangle
角の丸い四角形:ImageDraw.rounded_rectangle
円・楕円:ImageDraw.ellipse
正多角形:ImageDraw.regular_polygon
多角形:ImageDraw.polygon
について説明する。

 Pillow(PIL) を使うためにはインポートが必要。 PIL.ImageDrawモジュールは通常以下の形式でインポートされる。

from PIL import ImageDraw



1. 直線:ImageDraw.line

 直線を描画するlineの書式は以下の通り。

ImageDraw.line(xy, fill=None, width=0, joint=None)
引数 意味
xy 直線が通る座標のリスト
[(x, y), (x, y), ...] 又は [x, y, x, y, ...]の形式で指定
fill 塗りつぶしの色
width 線の幅
joint 'curve'を指定するとエッジが丸くなる

 線を引く際の実際の手順は、最初に Image オブジェクトを作成、さらに Draw オブジェクトを作成する。そして Draw オブジェクトに対してline メソッドを用いることで直線が作成される。
 灰色の背景に(x,y) = (10, 20), (100, 20), (120, 150)の3つの点を結ぶ幅5pixelの青い線を描画する例。

from PIL import Image, ImageDraw

# Imageオブジェクト作成
im = Image.new('RGB', (300, 200), 'gray')

# Drawオブジェクト作成
draw = ImageDraw.Draw(im)

# Drawオブジェクトに直線を描画
draw.line([(10, 20), (100, 20), (120, 150)], fill='blue', width=5)

# Imageオブジェクトの保存
im.save('line.png')

実行結果




2. 四角形:ImageDraw.rectangle

 四角形を描画するrectangleの書式は以下の通り。

ImageDraw.rectangle(xy, fill=None, outline=None, width=1)
引数 意味
xy 長方形の左上、右下の2点の座標
[(x0, y0), (x1, y1)] 又は[x0, y0, x1, y1]の形式で指定
fill 塗りつぶしの色
outline 線の色
width 線の幅

 黒い背景に左上・右下の角の座標(30, 40), (120, 150)、太さ3pixelのピンクの枠線、水色の塗りつぶしの四角形を描画。

from PIL import Image, ImageDraw

# Imageオブジェクト作成
im = Image.new('RGB', (300, 200), 'black')

# Drawオブジェクト作成
draw = ImageDraw.Draw(im)

# Drawオブジェクトに四角形を描画
draw.rectangle([(30,40), (120, 150)], 
                fill='skyblue', outline='pink', width=3)

# Imageオブジェクトの保存
im.save('rectangle.png')

実行結果




3. 角の丸い四角形:ImageDraw.rounded_rectangle

 角の丸い四角形を描画するrounded_rectangleの書式は以下の通り。

ImageDraw.rounded_rectangle(xy, radius=0, fill=None,
                            outline=None, width=1)
引数 意味
xy 長方形の左上、右下の2点の座標
[(x0, y0), (x1, y1)] 又は[x0, y0, x1, y1]の形式で指定
radius 角のRを指定
fill 塗りつぶしの色
outline 線の色
width 線の幅

 黒い背景に左上・右下の角の座標(50, 40), (200, 160)、角のR30pixel、線幅5pixelの角の丸い四角形を描画。

from PIL import Image, ImageDraw

# Imageオブジェクト作成
im = Image.new('RGB', (300, 200), (230, 200, 100))

# Drawオブジェクト作成
draw = ImageDraw.Draw(im)

# Drawオブジェクトに角の丸い四角形を描画
draw.rounded_rectangle([(50, 40), (200, 160)], radius=30, 
                     fill=(205, 255, 0), outline=(0, 0, 255), width=5)

# Imageオブジェクトの保存
im.save('rounded_rectangle.png')

実行結果




4. 円・楕円:ImageDraw.ellipse

 円、楕円を描画するellipseの書式は以下の通り。

ImageDraw.ellipse(xy, fill=None, outline=None, width=1)
引数 意味
xy 円が内接する四角形の左上、右下の2点の座標
[(x0, y0), (x1, y1)] 又は[x0, y0, x1, y1]の形式で指定
ここで x1 >= x0 および y1 >= y0
fill 塗りつぶしの色
outline 線の色
width 線の幅

 ピンクの背景に左上・右下の角の座標(20, 30), (160, 170)、太さ10pixelの白い枠線、黄緑の塗りつぶしの円を描画。

from PIL import Image, ImageDraw

# Imageオブジェクト作成
im = Image.new('RGB', (300, 200), 'pink')

# Drawオブジェクト作成
draw = ImageDraw.Draw(im)

# Drawオブジェクトに円を描画
draw.ellipse([(20, 30), (160, 170)], fill='yellowgreen', 
            outline='white', width=10)

# Imageオブジェクトの保存
im.save('circle.png')

実行結果




 外接する四角形が長方形の場合、楕円となる。

from PIL import Image, ImageDraw

# Imageオブジェクト作成
im = Image.new('RGB', (300, 200), 'pink')

# Drawオブジェクト作成
draw = ImageDraw.Draw(im)

# Drawオブジェクトに楕円を描画
draw.ellipse([(20, 30), (220, 170)], fill='yellowgreen', 
            outline='white', width=10)

# Imageオブジェクトの保存
im.save('circle.png')

実行結果




5. 正多角形:ImageDraw.regular_polygon

 正多角形を描画するregular_polygonの書式は以下の通り。

ImageDraw.regular_polygon(bounding_circle, n_sides, 
                          rotation=0, fill=None, outline=None)
引数 意味
bounding_circle 正多角形に外接する円の中心x, yと半径r
bounding_circle=(x, y, r) 又は ((x, y), r)の形式
n_sides 正多角形の辺の数。三角形は3、六角形は6
rotation 回転角度(°)。反時計回りが正、時計回りが負
fill 塗りつぶしの色
outline 線の色

 x,y = 150,100を中心に半径70pixelの外接円に接し、回転角度10度の正五角形を描画。

from PIL import Image, ImageDraw

# Imageオブジェクト作成
im = Image.new('RGB', (300, 200), (200, 200, 100))

# Drawオブジェクト作成
draw = ImageDraw.Draw(im)

# Drawオブジェクトに正多角形を描画
draw.regular_polygon((150, 100, 70), 5, 10, 
                     fill=(255, 255, 0), outline = (255, 0, 0))

# Imageオブジェクトの保存
im.save('regular_polygon.png')

実行結果




6. 多角形:ImageDraw.polygon

 多角形を描画するpolygonの書式は以下の通り。

ImageDraw.polygon(xy, fill=None, outline=None, width=1)
引数 意味
xy 多角形の頂点の座標
[(x, y), (x, y), ...]又は、[x, y, x, y, ...]
fill 塗りつぶしの色
outline 線の色
width 線の幅

 小麦色の背景、ネイビーの枠線、マゼンタの塗りつぶした頂点5点の多角形を描画。

from PIL import Image, ImageDraw

# Imageオブジェクト作成
im = Image.new('RGB', (300, 200), 'wheat')

# Drawオブジェクト作成
draw = ImageDraw.Draw(im)

# Drawオブジェクトに多角形を描画
draw.polygon([(20, 30), (100, 40), (240, 180), (150, 110), (50, 180)],
              fill='magenta', outline='navy')

# Imageオブジェクトの保存
im.save('polygon.png')

実行結果




7. リファレンス

Pillow (PIL Fork) > ImageDraw Module

使用バージョン:Python 3.7.0 / Pillow 7.1.2

0 件のコメント:

コメントを投稿