0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Python と Pillow を利用した正多角形の描画

Posted at

Python と Pillow を利用した正多角形の描画

開発環境

  • Visual Studio Code (Version: 1.84.2)
  • Python 3.12.0
  • Pillow 10.1.0

コード

import math
from PIL import Image, ImageDraw

# 定数
VERTEX_COUNT = 5  # 頂点の数
LINE_WIDTH = 1  # 辺の太さ
RADIUS = 96  # 外接円の半径(各頂点の中心からの距離)
IMAGE_WIDTH = 256  # 画像の横幅
IMAGE_HEIGHT = 256  # 画像の縦幅

# 色
COLOR_BLACK = (0, 0, 0)
COLOR_WHITE = (255, 255, 255)
COLOR_YELLOW = (255, 255, 0)

# 色の指定
BACKGROUND_COLOR = COLOR_WHITE  # 背景の色
EDGE_COLOR = COLOR_BLACK  # 辺の色
FILL_COLOR = COLOR_YELLOW  # 内部の領域の色

img = Image.new(mode="RGB", size=(IMAGE_WIDTH, IMAGE_HEIGHT), color=BACKGROUND_COLOR)
draw = ImageDraw.Draw(img)
origin_offset_x = IMAGE_WIDTH // 2  # 中心のX座標
origin_offset_y = IMAGE_HEIGHT // 2  # 中心のY座標

delta = 2.0 * math.pi / VERTEX_COUNT
vertices = []
for i in range(VERTEX_COUNT):
    vertices.append(
        (
            int(origin_offset_x + RADIUS * math.cos(delta * i)),
            int(origin_offset_y + RADIUS * math.sin(delta * i)),
        )
    )

draw.polygon(vertices, outline=EDGE_COLOR, fill=FILL_COLOR)
img.save("output.png")

出力された画像

  • VERTEX_COUNT = 5の場合

output.png

  • VERTEX_COUNT = 12の場合

output (1).png

参考資料

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?