以下の内容はhttps://dk521123.hatenablog.com/entry/2023/07/10/000000より取得しました。


【Python】Python ~ 画像処理 / Pillow ~

■ はじめに

Python の 画像処理ライブラリである Pillow について
徐々にまとめていく。

目次

【1】Pillow
【2】インストール
 1)トラブル
【3】サンプル

【1】Pillow

* Python の 画像処理ライブラリ

cf. Pillow (ピロー) = まくら?

【2】インストール

pip install Pillow

1)トラブル

 「pip install Pillow」後にPython実行しても
エラー「ModuleNotFoundError: No module named 'PIL'」が表示する

エラー内容

File "c:\xxxx\demo.py", line 5, in <module>
    from PIL import Image
ModuleNotFoundError: No module named 'PIL'

解決案
https://teratail.com/questions/376077?rss=all

# を参考に、、、

& C:/Users/User/Anaconda3/envs/py310/python.exe  -m pip install Pillow

【3】サンプル

generate_samples.py

from PIL import Image, ImageDraw, ImageFont


lines = [
    "会社名:株式会社テスト",
    "職種:ソフトウェアエンジニア",
    "勤務地:東京都渋谷区",
    "給与:年収500万円〜700万円"
]

#########
# To create an image with the same text
#########
img = Image.new("RGB", (600, 300), color=(255, 255, 255))
draw = ImageDraw.Draw(img)

font_path = "/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc"
font = ImageFont.truetype(font_path, 20)

y = 40
for line in lines:
    draw.text((40, y), line, fill=(0, 0, 0), font=font)
    y += 40

img.save("sample.png")
print("✅ sample.png created")

実行例

# Building Docker image completed.
docker build -t demo-env .

# Generating sample files...
docker run --rm -v "$PWD":/app demo-env python generate_samples.py

Dockerfile

FROM python:3.13-slim

RUN apt-get update && apt-get install -y --no-install-recommends \
    fonts-noto-cjk \
    build-essential \
    gcc \
    libgl1 \
    && apt-get clean && rm -rf /var/lib/apt/lists/*

WORKDIR /app

# For generate_samples.py
RUN pip install --no-cache-dir pillow

参考文献

https://camp.trainocate.co.jp/magazine/python-pillow/

関連記事

Python ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2014/08/07/231242
Python ~ 基本編 / 文字列 ~
https://dk521123.hatenablog.com/entry/2019/10/12/075251
Python + DOT言語で図作成するには
https://dk521123.hatenablog.com/entry/2023/06/14/174104
Python ~ PDF / PyFPDF ~
https://dk521123.hatenablog.com/entry/2023/07/19/001703
Python 〜 Tesseract OCR
https://dk521123.hatenablog.com/entry/2025/10/03/141326




以上の内容はhttps://dk521123.hatenablog.com/entry/2023/07/10/000000より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14