6
7

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 5 years have passed since last update.

【Python】Pillowで画像をresizeするときの処理時間の違い

Posted at

Pillowで画像をresizeするときに使用するフィルタによって,処理時間にどの程度差があるかを比較してみました。

使用するフィルタ
- NEAREST
- BOX
- BILINEAR
- HAMMING
- BICUBIC
- LANCZOS

上のフィルタほど低品質・高速,下ほど高品質・低速らしいです。

pipでインストール

pip install pillow

importと画像の準備

from PIL import Image

im = Image.open('sample.jpg')
im.size
Out[3]: (4032, 3024)

余談ですが,画像を一度だけ読み込んでresizeを複数回実行すると,最初だけ時間がかなりかかって2回目以降は極端に時間が短くなったので,都度画像をopenして比較します。

計測結果
In [5]: im = Image.open('sample.jpg')
   ...: %time resized_im = im.resize(size=(256, 256), resample=Image.NEAREST)
   ...: im = Image.open('sample.jpg')
   ...: %time resized_im = im.resize(size=(256, 256), resample=Image.HAMMING)
   ...: im = Image.open('sample.jpg')
   ...: %time resized_im = im.resize(size=(256, 256), resample=Image.BILINEAR)
   ...: im = Image.open('sample.jpg')
   ...: %time resized_im = im.resize(size=(256, 256), resample=Image.BOX)
   ...: im = Image.open('sample.jpg')
   ...: %time resized_im = im.resize(size=(256, 256), resample=Image.BICUBIC)
   ...: im = Image.open('sample.jpg')
   ...: %time resized_im = im.resize(size=(256, 256), resample=Image.LANCZOS)
   ...:
   ...:
Wall time: 223 ms
Wall time: 241 ms
Wall time: 239 ms
Wall time: 224 ms
Wall time: 264 ms
Wall time: 295 ms

ということで,最大で(NEARESTとLANCZOS)処理時間が20~30%ほど変わってきそうです。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?