|
|
|||||||||||||||||||||||||||||
|
matplotlib misc_Examples 04_image_thumbnail. |
H.Kamifuji . |
あなたは、既存の画像からサムネイルを生成するために matplotlib を使うことができます。 matplotlib は入力側でPNGファイルをネイティブサポートし、PIL がインストールされている場合は透過的に他のイメージタイプをサポートします
# -*- noplot -*-
"""
You can use matplotlib to generate thumbnails from existing images.
matplotlib natively supports PNG files on the input side, and other
image types transparently if your have PIL installed
"""
from __future__ import print_function
# build thumbnails of all images in a directory
import sys
import os
import glob
import matplotlib.image as image
if len(sys.argv) != 2:
print('Usage: python %s IMAGEDIR' % __file__)
raise SystemExit
indir = sys.argv[1]
if not os.path.isdir(indir):
print('Could not find input directory "%s"' % indir)
raise SystemExit
outdir = 'thumbs'
if not os.path.exists(outdir):
os.makedirs(outdir)
for fname in glob.glob(os.path.join(indir, '*.png')):
basedir, basename = os.path.split(fname)
outfile = os.path.join(outdir, basename)
fig = image.thumbnail(fname, outfile, scale=0.15)
print('saved thumbnail of %s to %s' % (fname, outfile))
|
当方の環境では動作しない。出力 msg は、下記$ py image_thumbnail.py images Traceback (most recent call last): File "image_thumbnail.py", line 31, inPython 3.11.2 見直しました。上記のコードでは、下記のエラーが発生します。 Traceback (most recent call last): File "_:\image_thumbnail.py", line 31, in <module> fig = image.thumbnail(fname, outfile, scale=0.15) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\_____\AppData\Local\Programs\Python\Python311\Lib\site-packages\matplotlib\image.py", line 1797, in thumbnail rows, cols, depth = im.shape ^^^^^^^^^^^^^^^^^ ValueError: not enough values to unpack (expected 3, got 2) matplotlib 内部のエラーのようです。matplotlib の改修(先祖帰りバグの改修)を待つしかない。 Python 3.11.6 (matplotlib 3.7.1) 及び Python 3.12.0 (matplotlib 3.8.1) では、上記と同様のエラーですが、下記の画像のみに発生するようです。画像データの不具合と考えるが、詳細は、不明。 ![]() 不具合画像を外して実行すると、正常に thumbs を作成できた。 PS M:\______\04_image_thumbnail> py image_thumbnail.py images saved thumbnail of images\ada.png to thumbs\ada.png saved thumbnail of images\grace_hopper.png to thumbs\grace_hopper.png ![]() ![]() 上記の事例は、scale=0.15 で 0.15 倍に resize するようです。 少し、実用できることを念頭に、縦横比を維持し、縦 64px の thumbs を作成できるよう、改修してみました。
# -*- noplot -*-
"""
You can use matplotlib to generate thumbnails from existing images.
matplotlib natively supports PNG files on the input side, and other
image types transparently if your have PIL installed
"""
from __future__ import print_function
# build thumbnails of all images in a directory
import sys
import os
import glob
import matplotlib.image as image
from PIL import Image
if len(sys.argv) != 2:
print('Usage: python %s IMAGEDIR' % __file__)
raise SystemExit
indir = sys.argv[1]
if not os.path.isdir(indir):
print('Could not find input directory "%s"' % indir)
raise SystemExit
outdir = 'thumbs_2'
if not os.path.exists(outdir):
os.makedirs(outdir)
for fname in glob.glob(os.path.join(indir, '*.png')):
basedir, basename = os.path.split(fname)
outfile = os.path.join(outdir, basename)
img = Image.open(fname)
img_resize = img.resize((64 * img.width // img.height, 64))
img_resize.save(outfile)
print('saved thumbnail of %s to %s' % (fname, outfile))
PIL を使用してサイズ変更するため、ダメな e.png , e4.png も戻し実行しました。PS E:\______\04_image_thumbnail> py image_thumbnail.py images saved thumbnail of images\ada.png to thumbs_2\ada.png saved thumbnail of images\e.png to thumbs_2\e.png saved thumbnail of images\e4.png to thumbs_2\e4.png saved thumbnail of images\grace_hopper.png to thumbs_2\grace_hopper.png ![]() ![]() ![]() ![]() PIL を使用してサイズ変更すると、透明設定も維持されています。 |
| misc_Examples code: image_thumbnail.py |
|