現在のディレクトリ内の画像のリストを提供するために、Python ジェネレーター関数を使用しています。ただし、関数がリスト全体を 1 回ではなく 2 回出力していることがわかりますが、その理由はわかりません。Python PIL ライブラリを使用してバッチ サムネイルを作成しています。
誰かが私を正しい方向に向けることができますか?
脚本:
import os
import sys
import Image
class ThumbnailGenerator:
def __init__(self, width, height, image_path, thumb_path):
self.width = width
self.height = height
self.image_path = image_path
self.thumb_path = "%s%s%s" % (self.image_path, os.sep, thumb_path)
def __call__(self):
self.__create_thumbnail_dir()
for filename, image in self.__generate_image_list():
try:
thumbnail = "%s%s%s" % (self.thumb_path, os.sep, filename)
image.thumbnail((self.width, self.height))
image.save(thumbnail, 'JPEG')
print "Thumbnail gemaakt voor: %s" % filename
except IOError:
print "Fout: thumbnail kon niet gemaakt worden voor: %s" % filename
def __generate_image_list(self):
for dirpath, dirnames, filenames in os.walk(self.image_path):
count = 0
for filename in filenames:
try:
image = Image.open(filename)
print '=========', count, filename
count += 1
yield (filename, image)
except IOError:
pass
def __create_thumbnail_dir(self):
try:
os.mkdir(self.thumb_path)
except OSError as exception:
print "Fout: %s" % exception
if __name__ == '__main__':
try:
thumbnail_generator = ThumbnailGenerator(80, 80, '.', 'thumbs')
thumbnail_generator()
except KeyboardInterrupt:
print 'Programma gestopt'
この時点でのスクリプトの出力 (いくつかのテスト イメージを含む) は次のとおりです。
========= 0 124415main_image_feature_380a_ys_full.jpg サムネイル画像: 124415main_image_feature_380a_ys_full.jpg ========= 1 60130main_image_feature_182_jwfull.jpg サムネイル画像: 60130main_image_feature_182_jwfull.jpg ========= 2 assetImage.jpg サムネイル画像: assetImage.jpg ========= 3 devcon-c1-image.gif Fout: サムネイル kon niet gemaakt worden voor: devcon-c1-image.gif ========= 4 image-646313.jpg サムネイル画像: image-646313.jpg ========= 5 Image-Schloss_Nymphenburg_Munich_CC.jpg サムネイル画像: Image-Schloss_Nymphenburg_Munich_CC.jpg ========= 6 image1w.jpg サムネイル画像: image1w.jpg ========= 7 New%20Image.jpg サムネイル画像: New%20Image.jpg ========= 8 samsung-gx20-image.jpg サムネイル画像: samsung-gx20-image.jpg ========= 9 samsung-image.jpg サムネイル画像: samsung-image.jpg ========= 0 124415main_image_feature_380a_ys_full.jpg サムネイル画像: 124415main_image_feature_380a_ys_full.jpg ========= 1 60130main_image_feature_182_jwfull.jpg サムネイル画像: 60130main_image_feature_182_jwfull.jpg ========= 2 assetImage.jpg サムネイル画像: assetImage.jpg ========= 3 devcon-c1-image.gif Fout: サムネイル kon niet gemaakt worden voor: devcon-c1-image.gif ========= 4 image-646313.jpg サムネイル画像: image-646313.jpg ========= 5 Image-Schloss_Nymphenburg_Munich_CC.jpg サムネイル画像: Image-Schloss_Nymphenburg_Munich_CC.jpg ========= 6 image1w.jpg サムネイル画像: image1w.jpg ========= 7 New%20Image.jpg サムネイル画像: New%20Image.jpg ========= 8 samsung-gx20-image.jpg サムネイル画像: samsung-gx20-image.jpg ========= 9 samsung-image.jpg サムネイル画像: samsung-image.jpg
それはあるべきですが:
========= 0 124415main_image_feature_380a_ys_full.jpg サムネイル画像: 124415main_image_feature_380a_ys_full.jpg ========= 1 60130main_image_feature_182_jwfull.jpg サムネイル画像: 60130main_image_feature_182_jwfull.jpg ========= 2 assetImage.jpg サムネイル画像: assetImage.jpg ========= 3 devcon-c1-image.gif Fout: サムネイル kon niet gemaakt worden voor: devcon-c1-image.gif ========= 4 image-646313.jpg サムネイル画像: image-646313.jpg ========= 5 Image-Schloss_Nymphenburg_Munich_CC.jpg サムネイル画像: Image-Schloss_Nymphenburg_Munich_CC.jpg ========= 6 image1w.jpg サムネイル画像: image1w.jpg ========= 7 New%20Image.jpg サムネイル画像: New%20Image.jpg ========= 8 samsung-gx20-image.jpg サムネイル画像: samsung-gx20-image.jpg ========= 9 samsung-image.jpg サムネイル画像: samsung-image.jpg
ご覧のとおり、ジェネレーター関数はリストを 2 回返しています (確認したところ、1 回だけ呼び出されます)。
@heikogerlach: os.walk は、現在のディレクトリのファイル名を調べているときにサムネイルを見つけることができず、サムネイルは現在のディレクトリの「thumb」というサブフォルダに書き込まれます。サムネイルを 'thumb' ディレクトリに書き込む前にリストが生成され、(WinPDB を使用して) サムネイルがリストに含まれていないことを確認しました。
@S.Lott: アドバイスありがとうございます。os.path.join は問題を修正しました。