Thumbnailaa kansio (.py)

empty 21.05.04 11:32

Tekee kansiosta thumbnailit (160*120) ja 640*480 kokoiset kuvat. Lue ohjeet skriptistä. Ei sisällä pahemmin virheen tarkistuksia.

 Tekstiversio  Arvo: 0 (0 ääntä)  Äänestä: +  -
###########################################################
# thumbnailDir.py, 20. 5. 2004                            #
# mail: tomiATkahjo.com                                   #
#                                                         #
# Requirements: Python and Python Imaging Library         #
#                                                         #
# Usage: thumbnailDir.py <path or directory>              #
#                                                         #
# Creates two new directories to specified path with one  #
# directory containing thumbnails (160, 120) and the      #
# other normal size images (640, 480).                    #
#                                                         #
# One should use an absolute path for relative paths      #
# may not work as expected.                               #
# Consider creating a shortcut to Python with commandline #
# argument to this script. Then just drag and drop        #
# directories onto the shortcut.                          #
###########################################################

import sys, os, os.path, string, Image

class Thumbnailer:
    "Thumbnail a directory of images"

    fileextensions = [".jpg", ".gif"]
   
    def __init__(self, fullpath):
        if os.path.exists(fullpath):
            self.outputdir = os.path.join(fullpath, "thumbs/")
            self.fullpath = fullpath
            self.images = self.readImages()
        else:
            raise IOError(2, "No such directory: "+fullpath)

    def readImages(self):
        files = os.listdir(self.fullpath)
        return filter(lambda x: string.lower(os.path.splitext(x)[1]) \
                      in self.fileextensions, files)

    def setOutputDir(self, fullpath, append=False):
        if append:
            self.outputdir = os.path.join(self.outputdir, fullpath)
        else:
            self.outputdir = fullpath

    def getFullpath(self):
        return self.fullpath

    def countImages(self):
        return len(self.images)

    def thumbnail(self, size):
        if not os.path.exists(self.outputdir):
            os.mkdir(self.outputdir)
        for image in self.images:
            im = Image.open(os.path.join(self.fullpath, image))
            im.thumbnail(size, Image.ANTIALIAS)
            im.save(os.path.join(self.outputdir, image))
                       
       
if __name__ == "__main__":
    print "+-----------------------------------------+"
    print "| ThumbnailFolder v0.1 by tomiATkahjo.com |"
    print "+-----------------------------------------+"
    print
    if len(sys.argv) == 2:
        t = Thumbnailer(sys.argv[1])
        print "Found", t.countImages(), "images in", t.getFullpath()
        print "Creating thumbnails..."
        t.thumbnail((160,120))
        print "Creating normal size images..."
        t.setOutputDir(os.path.join(t.getFullpath(), "normal"))
        t.thumbnail((640, 480))
        print "Done."
    else:
        print "usage:", os.path.basename(sys.argv[0]), "<path or directory>"
   
    print
    print "Press any key to quit"
    raw_input()