Imago - An Ajax Image Gallery - New supporting Tool
By: Jens in ajax | imago | tools
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.




on 12 August 2007 at 09:36 flow said …
nice work ... but your script doesn't write a completely correct XML - it misses out on the "imagePath" attribute to the simpleviewerGallery tag.
Easy to fix of course ... just thought I'd let you and everyone else know.
Keep up the good work and ... kudos to you , of course ;)
on 12 August 2007 at 10:37 Azarai said …
thanks and nice to get some feedback :-) It seems i broke the tool with my latest change in imago. In previous versions i didn't care about the imagePath attribute.
I fixed it in the download version.
on 1 August 2009 at 12:41 TDOT said …
Did anyone try to convert your script to other programming languages? I am looking for something like java or php or C# or even PERL... Thanks.
on 14 August 2009 at 07:16 Jens said …
I don't think so, but there do exist some similiar php scripts for simpleviewer and as its the same xml file format anything should be fine.