LoginSignup
8
3

More than 3 years have passed since last update.

Pillow resize と thumbnail の違い

Posted at

Pillow

Pillowは、Pythonの画像処理ライブラリです。
サムネイル画像作成など、画像を拡大・縮小する時に使われるメソッドを比較します。

resize

Image.resize(size, resample=3, box=None, reducing_gap=None)
サイズを変更したコピーを返す

Parameters

  • size: 変更したいサイズ (width, height)
  • resample: リサンプリングフィルタ (デフォルトは PIL.Image.BICUBIC)
    • PIL.Image.NEAREST
    • PIL.Image.BOX
    • PIL.Image.BILINEAR
    • PIL.Image.HAMMING
    • PIL.Image.BICUBIC
    • PIL.Image.LANCZOS
  • box: リサイズする画像の領域。長方形の領域のみ指定できる 未指定の場合、画像全体
    • 例: box=(100, 100, 300, 300)
  • reduce_gap: 段階的に画像サイズの変更、最適化をさせる (デフォルト: 2.0)

戻り値

Image object

thumbnail

Image.thumbnail(size, resample=3, reducing_gap=2.0)
アスペクト比を維持しながら、指定したサイズ以下の画像に縮小させる
- 例: 550x550の正方形画像に(600, 400)を指定した場合、400x400 となる

Parameters

  • size: 指定したsize以下に縮小する (width, height)
  • resample: リサンプリングフィルタ (デフォルトは PIL.Image.BICUBIC)
    • PIL.Image.NEAREST
    • PIL.Image.BOX
    • PIL.Image.BILINEAR
    • PIL.Image.HAMMING
    • PIL.Image.BICUBIC
    • PIL.Image.LANCZOS
  • reduce_gap: 段階的に画像サイズの変更、最適化をさせる (デフォルト: 2.0)

戻り値

なし(破滅的なメソッド)

比較

元画像(300x200)
test.png

resize

im = Image.open("test.png")
im_resized = im.resize(size=(150, 150))
im_resized.save('output.png')

output.png
150x150

thumbnail

im = Image.open("test.png")
im.thumbnail(size=(150, 150))
im.save('output2.png')

output2.png
150x100

size指定が大きい時

300x200 の画像を size=(400x500) を指定してみる

resize

im_resized = im.resize(size=(400, 500))

output3.png
400x500

thumbnail

im.thumbnail(size=(400, 500))

output4.png
300x200

参考

公式ドキュメント: https://pillow.readthedocs.io/en/stable/index.html

8
3
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
8
3