Imago - An Ajax Image Gallery - New supporting Tool
Recently i had the need of just putting the images in one directory into a Imago Image Gallery without using Picasa and caring about an image title. As i am a new fan of python, i hacked a script together that does the job. The script can be downloaded :Directory2Imago Image Gallery Script. If you only want to use the script you might skip here as i am now talking a bit of how i did it. So if your curious go ahead reading :-)
How does it work?
The script iterates over any file in a given directory and if its an image, it generate a thumbnail for it and adds it to the gallery.xml file. In the end we got an folder for uploading to a image gallery folder.
Any modules needed?
I am using PIL for generating the thumbnails. Anything else should be already in your python installation.
The important code part
I guess you already downloaded the script. The whole beginning is just parsing the options with getopts. If you are not familiar with it i recommend you take a look at getopt doc.
The main method is "create" as seen below. First we create or gallery directory and the 2 need subdirs for the thumbmnails and the images. Thereafter we iterate over each file in the given input dir and try to open it with PIL. If that fails, its no image file or at least none that PIL can handle. We ignore this error and continue with the next one. If its a valid image file we check if its landscape or portrait and calculate the size for the thumb, keeping the original aspect ratio. Then we thumbnail it with the best available algorithm and save it in the thumbnails folder. Now we copy the original image to the gallery image folder and add it to our gallery definition. At the end we save it to a gallery.xml file as UTF-8.
def create(self): self.__destinationDir += self.__galleryName if not os.path.exists(self.__destinationDir): os.mkdir(self.__destinationDir) os.mkdir(self.__destinationDir + "/thumbnails") os.mkdir(self.__destinationDir + "/images") for fileName in os.listdir (self.__indir): fileStats = os.stat ( self.__indir + fileName ) if not stat.S_ISDIR ( fileStats [ stat.ST_MODE ] ): try: originalImage = Image.open(self.__indir + fileName) width, height = originalImage.size if (width > height): size= self.__maxWidth, (height/(width/self.__maxWidth)) else: size= (width/(height/self.__maxHeight)), self.__maxHeight originalImage.thumbnail(size, Image.ANTIALIAS) originalImage.save(self.__destinationDir + "/thumbnails/" + fileName, "JPEG") shutil.copyfile(self.__indir + fileName,self.__destinationDir +"/images/" + fileName) self.__output += '<image><filename>' + fileName + '</filename><caption></caption></image>' except IOError: pass galleryXML = file(self.__destinationDir + '/gallery.xml', 'w') galleryXML.write((self.__xmlInfo + "\n" + self.__galleryStartPart1 + self.__galleryName + self.__galleryStartPart2 + "\n" + self.__output + "\n</simpleviewerGallery>").encode('utf-8')) galleryXML.close()
If you made some improvements, consider dropping us a note.