2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Python】Python PIL(Pillow)ガイド― 自動化・セキュリティ・ツール開発で“ちょうどいい”画像処理ライブラリ

2
Posted at

1. PIL とは何か

PIL(Python Imaging Library) は、Python における定番の画像処理ライブラリです。
現在はメンテナンスが終了しており、実際に使うのは後継の Pillow です。

PIL = 概念名
Pillow = 現役で使う実装

pip install pillow
from PIL import Image

2. なぜ今でも PIL(Pillow)なのか

PIL は最新の画像処理アルゴリズムを競うライブラリではありません。
しかし、エンジニアが現場で本当に困る部分を、最小コストで解決してくれます。

PIL が向いている用途

  • Selenium / Playwright のスクリーンショット処理
  • CAPTCHA や簡易 OCR 前処理
  • 自動化ツール・PoC
  • 画像のリサイズ・形式変換・注釈付け

「速く書けて、壊れにくい」
これが PIL の最大の価値です。


3. Image オブジェクトがすべての中心

from PIL import Image

img = Image.open("input.png")

よく使う属性

img.size    # (width, height)
img.format  # PNG / JPEG / WEBP
img.mode    # RGB / RGBA / L

mode の意味

mode 内容
RGB 通常のカラー画像
RGBA 透明度付き
L グレースケール
1 白黒

4. 開く・保存する・形式変換(超頻出)

PNG → JPG(透明を除去)

img = Image.open("image.png").convert("RGB")
img.save("image.jpg", quality=90)

圧縮して保存

img.save("out.jpg", optimize=True, quality=80)

👉 自動化やバッチ処理では毎日のように使います。


5. Resize / Crop(自動化の要)

サイズ変更

img.resize((300, 200))

アスペクト比を保つ(推奨)

img.thumbnail((300, 300))

切り抜き

img.crop((left, top, right, bottom))

Selenium で CAPTCHA や DOM の一部を切り出す場合、
crop は必須スキルです。


6. 画像に描画する(デバッグ・証跡に強い)

from PIL import ImageDraw, ImageFont

draw = ImageDraw.Draw(img)
draw.rectangle((50, 50, 200, 120), outline="red", width=3)
draw.text((60, 60), "Login", fill="blue")

フォント指定

font = ImageFont.truetype(
    "/System/Library/Fonts/Supplemental/Arial.ttf",
    24
)
draw.text((60, 100), "Password", font=font, fill="black")

用途例:

  • テスト結果のスクリーンショット注釈
  • セキュリティレポート用の強調表示
  • DOM 範囲の可視化

7. PIL × Selenium:実戦で最も使われる組み合わせ

import io
from PIL import Image

png = driver.get_screenshot_as_png()
img = Image.open(io.BytesIO(png))

CAPTCHA 部分だけ切り出す

captcha = img.crop((x1, y1, x2, y2))
captcha.save("captcha.png")

ブラウザ操作は Selenium、視覚処理は PIL
役割分担が非常にきれいです。


8. フィルタと前処理(OCR・セキュリティ用途)

from PIL import ImageFilter

gray = img.convert("L")
blur = gray.filter(ImageFilter.BLUR)
edge = gray.filter(ImageFilter.FIND_EDGES)
  • OCR の前処理
  • CAPTCHA のノイズ除去
  • 単純な特徴強調

このレベルなら OpenCV は不要です。


9. セキュリティ分野での PIL の実用例

CAPTCHA 対応

  • グレースケール化
  • 二値化
  • ノイズ軽減

画像ハッシュ(反爬・重複検知)

import imagehash
hash = imagehash.average_hash(img)

自動化証跡の生成

  • ログイン成功箇所を赤枠で表示
  • 時刻・URL を画像に直接描画

10. PIL と OpenCV の使い分け

用途 PIL OpenCV
自動化・スクリプト
学習コスト
OCR 前処理
高度な画像解析

結論

ツール開発・自動化 → PIL
画像アルゴリズム・CV → OpenCV


11. 実務でのベストプラクティス

  • JPG 保存前は必ず .convert("RGB")
  • find_pixel 的な手書きループは避ける
  • PIL は「画像の glue layer」と割り切る
  • Selenium と組み合わせて真価を発揮

まとめ

PIL(Pillow)は派手ではありません。
しかし、

「短いコードで、確実に仕事を終わらせる」

という点で、今も現役のライブラリです。

自動化・セキュリティ・社内ツールを書くエンジニアにとって、
PIL は一生モノの基礎スキルと言っていいでしょう。

2
4
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
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?