Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 189503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:10:41+00:00 2026-05-11T16:10:41+00:00

I’m using a python generator function to provide me with a list of images

  • 0

I’m using a python generator function to provide me with a list of images in the current directory. However I see the function is giving out the entire list twice instead of one time and I have no idea why. I’m using the Python PIL library to create batch thumbnails.

Can anyone point me in the right direction?

Script:


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'

The output of the script at this moment (with some test images) is:

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

While it should be:

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

As you can see the generator function is returning the list twice (I verified it and it gets called only once).

@heikogerlach:
os.walk cannot find the thumbnails as I’m walking the filenames of the current directory and the thumbnails get written to a sub-folder of the current directory called ‘thumb’. The list is generated before writing the thumbnails to the ‘thumb’ dir and I verified (using WinPDB) that the thumbnails are not included in the list.

@S.Lott:
Thanks for the advice. os.path.join fixed the problem.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T16:10:41+00:00Added an answer on May 11, 2026 at 4:10 pm

    In your debugging, print the full path. I think you’re walking the thumbs subdirectory after you walk the . directory.

    Also.

    class ThumbnailGenerator( object ):
    

    Usually works out better in the long run.

    Please do NOT use __ in front of your method names (generate_image_list and create_thumbnail_dir).

    Do not use "%s%s%s" % (self.image_path, os.sep, thumb_path) to make path names, use os.path.join.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 111k
  • Answers 111k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You MUST use the right encoder PngBitmapEncoder, JpegBitmapEncoder, GifBitmapEncoder. The… May 11, 2026 at 9:47 pm
  • Editorial Team
    Editorial Team added an answer The class that you posted doesn't appear to be your… May 11, 2026 at 9:47 pm
  • Editorial Team
    Editorial Team added an answer I believe the problem was that I did not understand… May 11, 2026 at 9:47 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.