Skip to content

Instantly share code, notes, and snippets.

@nac-39
Created June 7, 2023 15:05
Show Gist options
  • Save nac-39/80fc10d146b08f4d981c55ad8379744e to your computer and use it in GitHub Desktop.
Save nac-39/80fc10d146b08f4d981c55ad8379744e to your computer and use it in GitHub Desktop.
適当な大きさの画像に適当な文字を入れて生成するスクリプト
from PIL import Image, ImageDraw, ImageFont
import webcolors
def generate_img(height, width, text="sample", text_color="black", bg_color="gray"):
bg_color_hex = webcolors.name_to_hex(bg_color)
text_color_hex = webcolors.name_to_hex(text_color)
image = Image.new("RGB", (height, width), bg_color_hex)
# 画像に文字を入れる
draw = ImageDraw.Draw(image)
font = ImageFont.truetype("/path/to/font", 20)
draw.text(
(width // 2, height // 2), text, font=font, fill=text_color_hex, anchor="mm"
)
# 画像を保存する
image.save("test.png", "PNG")
if __name__ == "__main__":
generate_img(200, 200, "sample", "black", "gray")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment