diff --git a/Qalbum-descriptor.py b/Qalbum-descriptor.py
index e4d15fe..a541577 100755
--- a/Qalbum-descriptor.py
+++ b/Qalbum-descriptor.py
@@ -16,37 +16,15 @@
# along with this program. If not, see .
import sys,os
-import re
-import shutil
import subprocess
-from optparse import OptionParser
-from math import ceil
+from argparse import ArgumentParser
+import Qalbum
# (c) ville.rantanen@helsinki.fi
-imagesearch=re.compile('.*\.jpg$|.*\.jpeg$|.*\.gif$|.*\.png$|.*\.svg$|.*\.pdf$',re.I)
-excludepaths=re.compile('_med|_tn|\..*')
DESCFILE='descriptions.csv'
-def getimagelist(path):
- ''' Returns a list of images matching the regex '''
- list=os.listdir(path)
- imgs=[]
- for f in list:
- if (imagesearch.match(os.path.join(path,f))) and (os.path.isfile(os.path.join(path,f))):
- imgs.append(f)
- return imgs
-
-def getpathlist(path):
- ''' Returns a list of subfolders not matching the exclusion regex '''
- list=os.listdir(path)
- paths=[]
- for d in list:
- if (not excludepaths.match(d)) and (os.path.isdir(os.path.join(path,d))):
- paths.append(d)
- return paths
-
-def createdesc(path,list):
+def createdesc(path,list,options):
''' Runs imagemagick identify to create descriptions of images '''
if len(list)==0:
return
@@ -74,42 +52,43 @@ def create_description(infile,format):
output = idp.stdout.read()
return output
-def traverse(path):
+def traverse(path,options):
''' The recursive main function to create the thumbs and seek sub folders '''
print(path)
- pathlist=getpathlist(path)
- imagelist=getimagelist(path)
+ pathlist=Qalbum.getpathlist(path,options)
+ imagelist=Qalbum.getimagelist(path,options)
print(str(len(pathlist))+' paths')
print(str(len(imagelist))+' images')
- createdesc(path,imagelist)
+ createdesc(path,imagelist,options)
if options.recursive:
for p in pathlist:
- traverse(os.path.join(path,p))
+ traverse(os.path.join(path,p),options)
return
def execute():
''' Main execution '''
- usage='''Usage: %prog [options] folder
-folder is the root folder of the image album.'''
- parser=OptionParser(usage=usage)
- parser.add_option("-f",action="store_true",dest="force",default=False,
+ parser=ArgumentParser(version=Qalbum.__version__)
+ parser.add_argument("-f",action="store_true",dest="force",default=False,
help="Force rewriting of descriptions")
- parser.add_option("--format",type="str",dest="format",default="",
+ parser.add_argument("--format",type=str,dest="format",default="",
help="""Formatting string, see: http://www.imagemagick.org/script/escape.php.
Setting this option will override the presets""")
- parser.add_option("-r",action="store_true",dest="recursive",default=False,
+ parser.add_argument("-r",action="store_true",dest="recursive",default=False,
help="Recurse in to subfolders")
- parser.add_option("-p",type="int",dest="preset",default=1,
+ parser.add_argument("-p",type=int,dest="preset",default=1,
help="presets for descriptions. \"-p 0\" to get the list")
- global options
- (options,args)=parser.parse_args()
- if len(args) != 1 and options.preset!=0:
- parser.error("incorrect number of arguments")
+ parser.add_argument("startpath",type=str,action="store",default=os.path.abspath('.'),nargs='?',
+ help="Start path for recursion")
+ options=parser.parse_args()
+ options.startpath=os.path.abspath(options.startpath)
+ options=Qalbum.setupdefaultoptions(options)
+
presets=[
'%f: %[EXIF:DateTimeOriginal]',
'%f: %wx%h %[size]',
'%f
%[EXIF:DateTimeOriginal] %[EXIF:ExposureTime]s F%[EXIF:FNumber]']
if options.preset<1:
+ print "Presets:"
for row in range(len(presets)):
print row+1," ",presets[row]
sys.exit(0)
@@ -118,7 +97,7 @@ folder is the root folder of the image album.'''
parse.error("No such preset")
options.format=presets[options.preset-1]
- traverse(os.path.abspath(args[0]))
+ traverse(options.startpath,options)
return
execute()
diff --git a/Qalbum-thumbnailer.py b/Qalbum-thumbnailer.py
index 7e47321..12bb2ad 100755
--- a/Qalbum-thumbnailer.py
+++ b/Qalbum-thumbnailer.py
@@ -15,161 +15,57 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see .
-import sys,os
-import re
-import shutil
-import subprocess
-from optparse import OptionParser
-from math import ceil
+import Qalbum
+from argparse import ArgumentParser
+import os
# (c) ville.rantanen@helsinki.fi
-webfilesearch=re.compile('.*index.html$|.*gallerystyle.css$|.*galleryscript.js$|.*descriptions.csv$',re.I)
-imagesearch=re.compile('.*\.jpg$|.*\.jpeg$|.*\.gif$|.*\.png$|.*\.svg$|.*\.pdf$',re.I)
-vectorsearch=re.compile('.*\.svg$|.*\.pdf$',re.I)
-excludepaths=re.compile('_med|_tn|\..*')
-doublequotes=re.compile('"')
-singlequotes=re.compile("'")
-stripquotes=re.compile('^"|"$')
-def getimagelist(path):
- ''' Returns a list of images matching the regex '''
- list=os.listdir(path)
- imgs=[]
- for f in list:
- if (imagesearch.match(os.path.join(path,f))) and (os.path.isfile(os.path.join(path,f))):
- imgs.append(f)
- return imgs
-
-def getnonimagelist(path):
- ''' Returns a list of files not matching the image match regex '''
- list=os.listdir(path)
- files=[]
- for f in list:
- if (not webfilesearch.match(os.path.join(path,f))) and (not imagesearch.match(os.path.join(path,f))) and (os.path.isfile(os.path.join(path,f))):
- files.append(f)
- files.sort()
- return files
-
-def getpathlist(path):
- ''' Returns a list of subfolders not matching the exclusion regex '''
- list=os.listdir(path)
- paths=[]
- for d in list:
- if (not excludepaths.match(d)) and (os.path.isdir(os.path.join(path,d))):
- paths.append(d)
- return paths
-
-def createthumbs(path,list):
- ''' Runs imagemagick Convert to create medium sized and thumbnail images '''
- if len(list)==0:
- return
- if not os.path.exists(os.path.join(path,'_tn')):
- os.mkdir(os.path.join(path,'_tn'))
- if not os.path.exists(os.path.join(path,'_med')):
- os.mkdir(os.path.join(path,'_med'))
- n=1
- nsum=len(list)
- r=str(options.width)
- res=r+'x'+r+'>'
- for i in list:
- outmedium=os.path.join(path,'_med','med_'+i+'.jpg')
- outthumb=os.path.join(path,'_tn','tn_'+i+'.jpg')
- inpath=os.path.join(path,i)
- if (options.force) and os.path.exists(outmedium):
- os.unlink(outmedium)
- if (options.force) and os.path.exists(outthumb):
- os.unlink(outthumb)
- if (not os.path.exists(outmedium)):
- print('Medium.. '+i+' '+str(n)+'/'+str(nsum))
- create_medium_bitmap(inpath,outmedium,r,link=options.link,vector=vectorsearch.match(i))
- if (not os.path.exists(outthumb)):
- print('Thumbnail.. '+i+' '+str(n)+'/'+str(nsum))
- create_thumb_bitmap(outmedium,outthumb,vector=vectorsearch.match(i))
- n+=1
- return
-
-def create_medium_bitmap(infile,outfile,r,link=False,vector=False):
- if link:
- os.symlink(infile,outfile)
- return
- res=r+'x'+r+'>'
- if vector:
- convargs=['convert','-density','300x300',infile+'[0]','-background','white','-flatten','-resize',res,'-quality','97',outfile]
- else:
- convargs=['convert','-define','jpeg:size='+r+'x'+r,infile+'[0]','-background','white','-flatten','-resize',res,'-quality','85',outfile]
- convp=subprocess.call(convargs)
- return
-
-def create_thumb_bitmap(infile,outfile,vector=False):
- if vector:
- convargs=['convert','-density','300x300',infile,'-background','white','-flatten','-thumbnail','90x90^','-gravity','Center','-crop','90x90+0+0','+repage','-quality','75',outfile]
- else:
- convargs=['convert','-define','jpeg:size=300x300',infile,'-background','white','-flatten','-thumbnail','90x90^','-gravity','Center','-crop','90x90+0+0','+repage','-quality','75',outfile]
- convp=subprocess.call(convargs)
- return
-
-def cleanthumbs(path):
- ''' clears _med and _tn for unused thumbs '''
- print('clearing unused thumbs...')
- if os.path.exists(os.path.join(path,'_tn')):
- clearfolder(path,os.path.join(path,'_tn'),re.compile("(^tn_)(.*)(.jpg)"))
- if os.path.exists(os.path.join(path,'_med')):
- clearfolder(path,os.path.join(path,'_med'),re.compile("(^med_)(.*)(.jpg)"))
-
- return
-def clearfolder(path,tnpath,regex):
- ''' clears given folder '''
- list=getimagelist(tnpath)
- for i in list:
- f=regex.match(i)
- try:
- if not os.path.exists(os.path.join(path,f.group(2))):
- print('removing '+i)
- os.remove(os.path.join(tnpath,i))
- except:
- continue
- return
-
-def traverse(path):
+def traverse(path,options):
''' The recursive main function to create the thumbs and seek sub folders '''
print(path)
- pathlist=getpathlist(path)
- imagelist=getimagelist(path)
+ pathlist=Qalbum.getpathlist(path,options)
+ imagelist=Qalbum.getimagelist(path,options)
print(str(len(pathlist))+' paths')
print(str(len(imagelist))+' images')
if options.clean:
- cleanthumbs(path)
- createthumbs(path,imagelist)
+ Qalbum.cleanthumbs(path)
+ Qalbum.createthumbs(path,imagelist,options)
if options.recursive:
for p in pathlist:
- traverse(os.path.join(path,p))
+ traverse(os.path.join(path,p),options)
return
+def setupoptions():
+ ''' Setup options '''
+ usage='''Usage: %(prog)s [options] folder
+folder is the root folder of the image album (defaults to current folder).'''
+ parser=ArgumentParser(description=usage,version=Qalbum.__version__)
+ parser.add_argument("-f",action="store_true",dest="force",default=False,
+ help="Force regeneration of thumbnails and medium sized")
+ parser.add_argument("-c",action="store_true",dest="clean",default=False,
+ help="Clean unused thumbnails")
+ parser.add_argument("-l",action="store_true",dest="link",default=False,
+ help="Create symbolic link as medium size image instead of downscaling one.")
+ parser.add_argument("-r",action="store_true",dest="recursive",default=False,
+ help="Recurse in to subfolders")
+ parser.add_argument("-w",type=int,dest="width",default=850,
+ help="Medium image size (Default: %(default)s)")
+ parser.add_argument("startpath",type=str,action="store",default=os.path.abspath('.'),nargs='?',
+ help="Start path for recursion")
+ options=parser.parse_args()
+ options.startpath=os.path.abspath(options.startpath)
+ options=Qalbum.setupdefaultoptions(options)
+ return options
+
def execute():
''' Main execution '''
- usage='''Usage: %prog [options] folder
-folder is the root folder of the image album.'''
- parser=OptionParser(usage=usage)
- parser.add_option("-f",action="store_true",dest="force",default=False,
- help="Force regeneration of thumbnails and medium sized")
- parser.add_option("-c",action="store_true",dest="clean",default=False,
- help="Clean unused thumbnails")
- parser.add_option("-l",action="store_true",dest="link",default=False,
- help="Create symbolic link as medium size image instead of downscaling one.")
- parser.add_option("-r",action="store_true",dest="recursive",default=False,
- help="Recurse in to subfolders")
- parser.add_option("-w",type="int",dest="width",default=850,
- help="Medium image size")
- global options
- (options,args)=parser.parse_args()
- if len(args) != 1:
- parser.error("incorrect number of arguments")
- startpath=os.path.abspath(args[0])
- traverse(startpath)
+ options=setupoptions()
+ traverse(options.startpath,options)
return
-execute()
-sys.exit(0)
-
+if __name__ == "__main__":
+ execute()
+
diff --git a/Qalbum.py b/Qalbum.py
index 8aa56db..e7b5819 100755
--- a/Qalbum.py
+++ b/Qalbum.py
@@ -26,7 +26,7 @@ from datetime import datetime
# (c) ville.rantanen@helsinki.fi
-__version__='1.6'
+__version__='1.7b'
webfilesearch=re.compile('.*index.html$|.*gallerystyle.css$|.*galleryscript.js$|.*descriptions.csv$|\..*',re.I)
imagesearch=re.compile('.*\.jpg$|.*\.jpeg$|.*\.gif$|.*\.png$|.*\.svg$|.*\.pdf$',re.I)
@@ -58,17 +58,20 @@ def getfooter():