Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'simpler img script' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
simpler img script
Authored by: Florian Bösch on Dec 27, '04 11:45:36AM
I would propose a much simpler, cleaner, portable and more powerfull script. something like:

#!/usr/bin/python
try: import sys, os, Image
except: print >> sys.stderr, 'Please install Python Imaging Library to run this script.'
else:
    if __name__=='__main__':
        if len(sys.argv) == 1: outstream = sys.stdout
        elif len(sys.argv) == 3 and sys.argv[1] == '-f': outstream = open( sys.argv[2], 'w' )
        else:
            print '\nUSAGE : "%s" <output filename>'%sys.argv[0]
            sys.exit( -1 )
        
        extensions = ['JPG', 'jpg', 'JPEG', 'jpeg', 'GIF', 'gif', 'PNG', 'png']
        file_list = [name for name in os.listdir('.') if name.split('.')[-1] in extensions]

        for image_name in file_list:
            try:
                width, height = Image.open( image_name ).size
                print >> outstream, '<img src="%s" width="%s" height="%s">'%(image_name,width,height)
            except:
                print >> sys.stderr, 'Unexpected error for file %s.'%image_name


[ Reply to This | # ]