生成器函数执行两次?

时间:2009-04-17 14:35:52

标签: python generator

我正在使用python生成器函数为我提供当前目录中的图像列表。但是我看到这个函数是两次而不是一次给出整个列表,我不明白为什么。我正在使用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
Thumbnail gemaakt voor: 124415main_image_feature_380a_ys_full.jpg
========= 1 60130main_image_feature_182_jwfull.jpg
Thumbnail gemaakt voor: 60130main_image_feature_182_jwfull.jpg
========= 2 assetImage.jpg
Thumbnail gemaakt voor: assetImage.jpg
========= 3 devcon-c1-image.gif
Fout: thumbnail kon niet gemaakt worden voor: devcon-c1-image.gif
========= 4 image-646313.jpg
Thumbnail gemaakt voor: image-646313.jpg
========= 5 Image-Schloss_Nymphenburg_Munich_CC.jpg
Thumbnail gemaakt voor: Image-Schloss_Nymphenburg_Munich_CC.jpg
========= 6 image1w.jpg
Thumbnail gemaakt voor: image1w.jpg
========= 7 New%20Image.jpg
Thumbnail gemaakt voor: New%20Image.jpg
========= 8 samsung-gx20-image.jpg
Thumbnail gemaakt voor: samsung-gx20-image.jpg
========= 9 samsung-image.jpg
Thumbnail gemaakt voor: samsung-image.jpg
========= 0 124415main_image_feature_380a_ys_full.jpg
Thumbnail gemaakt voor: 124415main_image_feature_380a_ys_full.jpg
========= 1 60130main_image_feature_182_jwfull.jpg
Thumbnail gemaakt voor: 60130main_image_feature_182_jwfull.jpg
========= 2 assetImage.jpg
Thumbnail gemaakt voor: assetImage.jpg
========= 3 devcon-c1-image.gif
Fout: thumbnail kon niet gemaakt worden voor: devcon-c1-image.gif
========= 4 image-646313.jpg
Thumbnail gemaakt voor: image-646313.jpg
========= 5 Image-Schloss_Nymphenburg_Munich_CC.jpg
Thumbnail gemaakt voor: Image-Schloss_Nymphenburg_Munich_CC.jpg
========= 6 image1w.jpg
Thumbnail gemaakt voor: image1w.jpg
========= 7 New%20Image.jpg
Thumbnail gemaakt voor: New%20Image.jpg
========= 8 samsung-gx20-image.jpg
Thumbnail gemaakt voor: samsung-gx20-image.jpg
========= 9 samsung-image.jpg
Thumbnail gemaakt voor: samsung-image.jpg

虽然它应该是:

========= 0 124415main_image_feature_380a_ys_full.jpg
Thumbnail gemaakt voor: 124415main_image_feature_380a_ys_full.jpg
========= 1 60130main_image_feature_182_jwfull.jpg
Thumbnail gemaakt voor: 60130main_image_feature_182_jwfull.jpg
========= 2 assetImage.jpg
Thumbnail gemaakt voor: assetImage.jpg
========= 3 devcon-c1-image.gif
Fout: thumbnail kon niet gemaakt worden voor: devcon-c1-image.gif
========= 4 image-646313.jpg
Thumbnail gemaakt voor: image-646313.jpg
========= 5 Image-Schloss_Nymphenburg_Munich_CC.jpg
Thumbnail gemaakt voor: Image-Schloss_Nymphenburg_Munich_CC.jpg
========= 6 image1w.jpg
Thumbnail gemaakt voor: image1w.jpg
========= 7 New%20Image.jpg
Thumbnail gemaakt voor: New%20Image.jpg
========= 8 samsung-gx20-image.jpg
Thumbnail gemaakt voor: samsung-gx20-image.jpg
========= 9 samsung-image.jpg
Thumbnail gemaakt voor: samsung-image.jpg

正如您所看到的那样,生成器函数返回列表两次(我验证了它并且只调用了一次)。

@heikogerlach: 当我走过当前目录的文件名时,os.walk找不到缩略图,缩略图被写入当前目录的一个名为'thumb'的子文件夹中。在将缩略图写入'thumb'目录之前生成该列表,并且我验证(使用WinPDB)缩略图未包含在列表中。

@美国洛特: 感谢您的建议。 os.path.join解决了这个问题。

2 个答案:

答案 0 :(得分:3)

在调试中,打印完整路径。在您走过thumbs目录后,我认为您正在走.子目录。

另外。

class ThumbnailGenerator( object ):

从长远来看,通常效果更好。

请不要在方法名称前面使用__(generate_image_list和create_thumbnail_dir)。

请勿使用"%s%s%s" % (self.image_path, os.sep, thumb_path)制作路径名,请使用os.path.join

答案 1 :(得分:0)

您的缩略图位于self.image_path的子目录中,并且与原始图像具有相同的名称。你可以在创建时检查walk是否找到了thumnails吗?只需打印图像的路径和名称。