xinclude: photoalbum caption and gallery
authorFredrik Unger <fred@tree.se>
Fri, 27 Feb 2015 09:20:19 +0000 (10:20 +0100)
committerFredrik Unger <fred@tree.se>
Fri, 27 Feb 2015 09:20:19 +0000 (10:20 +0100)
Adding the caption listed in description. Generating
a gallery that then also can be used by the stylesheets.

xinclude/photoalbum.py

index ec2f91f1e0ada1ababbb2cce5359a0e7bacb2d88..b33f4f04c517da0874b59dcf774385a7f8d9a771 100755 (executable)
@@ -6,7 +6,8 @@ import sys
 import glob
 from shutil import rmtree,copytree
 from PIL import Image
-
+from libxmp import consts
+from libxmp import XMPFiles
 from lxml import etree
 from lxml.builder import ElementMaker
 
@@ -14,32 +15,63 @@ from treecutter import constants as const
 
 class PhotoAlbum(object):
     def __init__(self, uri):
-        self.uri = uri
+        self.uri = unicode(uri)
+        self.albumid = abs(hash(self.uri))
         self.filelist = []
+        self.filelist_thumb = []
+        self.filelist_slider = []
 
-    def thumbnail(self,infile,size):
-        file, ext = os.path.splitext(infile)
-        im = Image.open(infile)
-        im.thumbnail(size, Image.ANTIALIAS)
-        im.save(file + "."+str(size(0))+"x"+str(size(1))+, "JPG")
+    def format(self):
+        for infile in self.filelist:
+            size = (50, 50)
+            outfile, ext = os.path.splitext(infile)
+            outfile = outfile+"."+str(size[0])+"x"+str(size[1])+ext
+            if not os.path.exists(outfile):
+                im = Image.open(infile)
+                im.thumbnail(size, Image.ANTIALIAS)
+                im.save(outfile)
+                self.filelist_thumb.append(outfile)
+            size = (700, 438)
+            outfile, ext = os.path.splitext(infile)
+            outfile = outfile+"."+str(size[0])+"x"+str(size[1])+ext
+            if not os.path.exists(outfile):
+                im = Image.open(infile)
+                im.thumbnail(size, Image.ANTIALIAS)
+                bg = Image.new('RGBA', size, (0, 0, 0, 0))
+                bg.paste(im,((size[0]-im.size[0])/2, (size[1]-im.size[1])/2))
+                bg.save(outfile)
+                self.filelist_slider.append(outfile)
 
     def files(self):
-        rootdirs = [self.uri+'/'+d for d in dirs]
-        for rootdir,d in zip(rootdirs,dirs):
-            if os.path.exists(d):
-                rmtree(d)
-            copytree(rootdir, d)
-        for d in dirs:
-            for root, subdir, files in os.walk(d):
-                for file in files:
-                    self.filelist.append(os.path.join(root,file))
+        d = self.uri
+        for root, subdir, files in os.walk(d):
+            for f in files:
+                self.filelist.append(os.path.join(root,f))
 
     def db_xml(self):
         db = ElementMaker(namespace=const.DB_NS, nsmap=const.NSMAP)
-        co = db.para()
-        for f in self.filelist:
-            co.append(db.filename(db.link(f,**{const.XLINK+"href": f})))
-        return db.para(co)
+        sl = db.itemizedlist(**{const.XML+"id": "slider"})
+        cnt = 0
+        for fs, f in zip(self.filelist_slider, self.filelist):
+            cnt = cnt + 1
+            xmpfile = XMPFiles(file_path=f)
+            xmp = xmpfile.get_xmp()
+            if xmp.does_property_exist(consts.XMP_NS_DC, 'description[1]'):
+                cap = xmp.get_property(consts.XMP_NS_DC, 'description[1]')
+            else:
+                cap = "Beskrivning saknas"
+            xmpfile.close_file()
+            sl.append(
+                db.listitem(db.mediaobject(
+                        db.imageobject(db.imagedata(fileref=fs)),
+                        db.caption(db.para(cap, db.link("[]",**{const.XLINK+"href": f})))),**{const.XML+"id": "p%x%d" % (self.albumid,cnt)}))
+
+        th = db.itemizedlist(**{const.XML+"id": "thumb"})
+        cnt = 0
+        for f in self.filelist_thumb:
+            cnt = cnt + 1
+            th.append(db.listitem(db.para(db.link(db.inlinemediaobject(db.imageobject(db.imagedata(fileref=f))),**{const.XLINK+"href": "#p%x%d" % (self.albumid, cnt)}))))
+        return db.informalfigure(sl,th,**{const.XML+"id": "box"})
 
 def recursively_empty(e):
     if e.text:
@@ -63,9 +95,11 @@ if __name__ == "__main__":
 
     album = PhotoAlbum(argument)
     album.files()
+    album.format()
     axml = album.db_xml()
-    clean_db(axml)
+#    clean_db(axml)
 
     #print(etree.tostring(cxml, pretty_print=True))
-    #sys.stdout.write(out.encode('utf-8'))
+    #sys.stderr.write(axml.encode('utf-8'))
+    #sys.stderr.write(etree.tostring(axml,encoding='UTF-8',pretty_print=True))
     sys.stdout.write(etree.tostring(axml,encoding='UTF-8',pretty_print=True))