画像をリサイズする、拡大、縮小

画像をリサイズするには Pillow の resize を使うのが簡単です。

from PIL import Image
import os

# サイズ変更対象の画像ファイル.
im = Image.open( "c:/tmp/test_1024_0768.bmp" )
w = im.width
h = im.height

# 引数の補間モードにてリサイズする、ここでは半分に縮小する.
w_small = w >> 1
h_small = h >> 1
im00 = im.resize(( w_small, h_small ), resample = Image.NEAREST )
im01 = im.resize(( w_small, h_small ), resample = Image.BOX )
im02 = im.resize(( w_small, h_small ), resample = Image.BILINEAR )
im03 = im.resize(( w_small, h_small ), resample = Image.HAMMING )
im04 = im.resize(( w_small, h_small ), resample = Image.BICUBIC )
im05 = im.resize(( w_small, h_small ), resample = Image.LANCZOS )

# 引数の補間モードにてリサイズする、ここでは倍に拡大する.
w_large = w << 1
h_large = h << 1
im10 = im.resize(( w_large, h_large ), resample = Image.NEAREST )
im11 = im.resize(( w_large, h_large ), resample = Image.BOX )
im12 = im.resize(( w_large, h_large ), resample = Image.BILINEAR )
im13 = im.resize(( w_large, h_large ), resample = Image.HAMMING )
im14 = im.resize(( w_large, h_large ), resample = Image.BICUBIC )
im15 = im.resize(( w_large, h_large ), resample = Image.LANCZOS )

# ファイルを配置するディレクトリを作成する.
the_dir = "c:/tmp/resize"
os.makedirs( the_dir, exist_ok = True)

# ファイルパス(縮小用)を生成する.
fp00 = os.path.join( the_dir, "small_NEAREST.bmp" ) 
fp01 = os.path.join( the_dir, "small_BOX.bmp" )
fp02 = os.path.join( the_dir, "small_BILINEAR.bmp" )
fp03 = os.path.join( the_dir, "small_HAMMING.bmp" )
fp04 = os.path.join( the_dir, "small_BICUBIC.bmp" )
fp05 = os.path.join( the_dir, "small_LANCZOS.bmp" )

# ファイルパス(拡大用)を生成する.
fp10 = os.path.join( the_dir, "large_NEAREST.bmp" ) 
fp11 = os.path.join( the_dir, "large_BOX.bmp" )
fp12 = os.path.join( the_dir, "large_BILINEAR.bmp" )
fp13 = os.path.join( the_dir, "large_HAMMING.bmp" )
fp14 = os.path.join( the_dir, "large_BICUBIC.bmp" )
fp15 = os.path.join( the_dir, "large_LANCZOS.bmp" )

# 縮小した画像をファイル保存する.
im00.save( fp00 )
im01.save( fp01 )
im02.save( fp02 )
im03.save( fp03 )
im04.save( fp04 )
im05.save( fp05 )

# 拡大した画像をファイル保存する.
im10.save( fp10 )
im11.save( fp11 )
im12.save( fp12 )
im13.save( fp13 )
im14.save( fp14 )
im15.save( fp15 )

# ファイルパスを表示する.
print( fp00 )
print( fp01 )
print( fp02 )
print( fp03 )
print( fp04 )
print( fp05 )

# ファイルパスを表示する.
print( fp10 )
print( fp11 )
print( fp12 )
print( fp13 )
print( fp14 )
print( fp15 )

print( "finish." )

下記が実行結果です。
表示されているディレクトリパスに、拡大と縮小の画像が配置されています。

PS c:\tmp> python test.py
c:/tmp/resize\small_NEAREST.bmp
c:/tmp/resize\small_BOX.bmp
c:/tmp/resize\small_BILINEAR.bmp
c:/tmp/resize\small_HAMMING.bmp
c:/tmp/resize\small_BICUBIC.bmp
c:/tmp/resize\small_LANCZOS.bmp
c:/tmp/resize\large_NEAREST.bmp
c:/tmp/resize\large_BOX.bmp
c:/tmp/resize\large_BILINEAR.bmp
c:/tmp/resize\large_HAMMING.bmp
c:/tmp/resize\large_BICUBIC.bmp
c:/tmp/resize\large_LANCZOS.bmp
finish.

実行させると下記のような警告がでます。これは実行に失敗したわけではありません。

2023-07-01 以降の Pillow バージョン10 では Image.NEAREST ではなく Resampling.NEAREST と記述せよ、とのことです。その他の BOX, BILINEAR, HAMMING, BICUBI, LANCZOS もすべて同様です。

DeprecationWarning: NEAREST is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.NEAREST or Dither.NONE instead.
DeprecationWarning: BOX is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.BOX instead.
DeprecationWarning: BILINEAR is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.BILINEAR instead.
DeprecationWarning: HAMMING is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.HAMMING instead.
DeprecationWarning: BICUBIC is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.BICUBIC instead.
DeprecationWarning: LANCZOS is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.LANCZOS instead.

この警告がでることによって問題が生じるならば、warnings モジュールを用いて ignore することにより、警告メッセージを表示させないようにしてください。第2引数が無視したい警告のカテゴリを指定します。"DeprecationWarning" は "非推奨の警告" ということです。

# 警告を出さないためには冒頭にこの2行を追加せよ.
import warnings
warnings.simplefilter( "ignore", DeprecationWarning )

from PIL import Image
import os

あまりお勧めできませんが DeprecationWarning でも FutureWarning でも、その他もろもろの Warning は全て無視するならば、下記のように第2引数を省略すればいいです。

# 警告を出さないためには冒頭にこの2行を追加せよ.
import warnings
warnings.simplefilter( "ignore" )

from PIL import Image
import os