#!/usr/bin/python # # Little script to generate image galleries for use with original PHP backend # by Jakub 'jimmac' Steiner. # When comments from the gthumb image manager are available, use them. # # uses Gnome's zenity for user dialogs # uses convert from ImageMagick # # Differences with the perl version: # - Use only convert to create the thumbnails. # - Do not set the time of the gallery folder with the time of an image file. # - Do not create the medium size thumbnails (only a comment block to remove # and you have it). # - Do not create a zip archive of the HQ files. # - Use the comments from gthumb when available. # # (c) 2005 Loic d'Anterroches (http://xhtml.net) # # Based on webgallery-zenity.pl # (c) 2003-2004 Jakub 'jimmac' Steiner, (c) 2003-2004 Colin Marquardt # based on webgallery.pl by Tuomas Kuosmanen # # INSTALL # # Copy this file into ~/.gnome2/nautilus-scripts/ as b-origin.py and set it # as executable. # Set the gthumbversion value to your version of gthumb # import sys import commands import gzip import os import re import shutil # Put only the major.minor version number. # for example 2.4 or 2.6 gthumbversion = 2.6 def getcomments(image, number): """Get the comments for an image. The comments are coming from the xml file created by gthumb. If no comment file, it simply return "Image number" """ cwd = os.getcwd() if gthumbversion > 2.4: commentfile = os.path.join(cwd,".comments", img+".xml") else: commentfile = os.path.join(os.path.expanduser("~"), ".gnome2/gthumb/comments", cwd[1:], img+".xml") if os.path.isfile(commentfile): gzipfile = gzip.GzipFile(commentfile, "rb", 3) content = gzipfile.read() start = content.find("") end = content.find("", start) return content[start+6:end] else: return "Image "+str(number) def createfolders(outputdir): """Create the folders for the thumbnails and comments within the 'outputdir' folder. """ errors = "" if not os.path.isdir(outputdir): try: os.mkdir(outputdir) except: errors = errors + "Could not create: "+outputdir+"\n" if not os.path.isdir(os.path.join(outputdir,'thumbs')): try: os.mkdir(os.path.join(outputdir,'thumbs')) except: errors = errors + "Could not create: "+os.path.join(outputdir,'thumbs')+"\n" if not os.path.isdir(os.path.join(outputdir,'lq')): try: os.mkdir(os.path.join(outputdir,'lq')) except: errors = errors + "Could not create: "+os.path.join(outputdir,'lq')+"\n" # if not os.path.isdir(os.path.join(outputdir,'mq')): # try: # os.mkdir(os.path.join(outputdir,'mq')) # except: # errors = errors + "Could not create: "+os.path.join(outputdir,'mq')+"\n" if not os.path.isdir(os.path.join(outputdir,'hq')): try: os.mkdir(os.path.join(outputdir,'hq')) except: errors = errors + "Could not create: "+os.path.join(outputdir,'hq')+"\n" if not os.path.isdir(os.path.join(outputdir,'comments')): try: os.mkdir(os.path.join(outputdir,'comments')) except: errors = errors + "Could not create: "+os.path.join(outputdir,'comments')+"\n" # if not os.path.isdir(os.path.join(outputdir,'zip')): # try: # os.mkdir(os.path.join(outputdir,'zip')) # except: # errors = errors + "Could not create: "+os.path.join(outputdir,'zip')+"\n" if len(errors)>0: commands.getoutput("zenity --error --title \"Giving Up\" --text \"Fatal Error\n\n"+errors+"\""); sys.exit(500) if len(sys.argv) <= 1: commands.getoutput("zenity --error --title \"\" --text \"No selected images.\nYou have to select images to work on.\"") sys.exit(500) convert = "convert" #Image converter folder = "web-gallery" #Output folder (as a subfolder of current folder) scaler = commands.getoutput("which "+convert) scaler = scaler.strip() if len(scaler)==0: commands.getoutput("zenity --error --title \"\" --text \"No scaling program\n\nYou need to have '"+convert+"' available.\"") sys.exit() files = sys.argv[1:] files.sort() numoffiles = len(files) numofincrements = 5 # 5 increments per file (as we are expecting to create 5 files for each image) increment = 100 / ( numoffiles * numofincrements) progress = 0 outputdir = os.path.join(os.getcwd(), folder) createfolders(outputdir) pbar = os.popen("zenity --progress --auto-close --title=\"\" --text=\"Scaling images, please wait\"", "w", 0) #0 is for unbuffered open i=1 # not at all a pythonic way to iterate (direct perl translation by # a python newbie). for img in files: if os.path.isdir(img): progress = progress + (increment * numofincrements) pbar.write(str(progress)+"\n") continue filetype = commands.getoutput("file "+img) if not re.search("image data", filetype, re.IGNORECASE): progress = progress + (increment * numofincrements) pbar.write(str(progress)+"\n") continue reply = "" #thumbnails reply = reply + commands.getoutput(scaler+" -geometry 120x120 -quality 60 \""+img+"\" \""+os.path.join(outputdir,"thumbs/img-"+str(i)+".jpg")+"\"") progress = progress + increment pbar.write(str(progress)+"\n") #LQ size reply = reply + commands.getoutput(scaler+" -geometry 640x480 -quality 90 \""+img+"\" \""+os.path.join(outputdir,"lq/img-"+str(i)+".jpg")+"\"") progress = progress + increment pbar.write(str(progress)+"\n") ## #MQ size ## reply = reply + commands.getoutput(scaler+" -geometry 800x600 -quality 75 \""+img+"\" \""+os.path.join(outputdir,"mq/img-"+str(i)+".jpg")+"\"") ## progress = progress + increment ## pbar.write(str(progress)+"\n") #HQ size (just copy the original) shutil.copy(img, os.path.join(outputdir,"hq/img-"+str(i)+".jpg")) progress = progress + increment pbar.write(str(progress)+"\n") #Comment comm = open(os.path.join(outputdir,"comments/"+str(i)+".txt"),'w') comm.write(""+getcomments(img,i)+"") comm.close() progress = progress + increment pbar.write(str(progress)+"\n") i = i + 1 #next image if len(reply) > 0: pbar.write("100\n") pbar.close() commands.getoutput("zenity --error --title \"\" --text \"Fatal Error\n\n"+reply+"\"") sys.exit() pbar.write("100\n") try: pbar.close() except IOError: pass # ------------------------------------------------------------------------ comm = open(os.path.join(outputdir,".htaccess"),"w") comm.write("\n\tdeny from all\n\n") comm.close()