photoalbum: rewote to use the image object
authorFredrik Unger <fred@tree.se>
Tue, 31 Mar 2015 12:26:49 +0000 (14:26 +0200)
committerFredrik Unger <fred@tree.se>
Tue, 31 Mar 2015 12:26:49 +0000 (14:26 +0200)
Utilizing the image transformation code in the image class
to deal with generating the phonoalbum instead of having
two implementations transforming files.

xinclude/photoalbum.py

index b33f4f04c517da0874b59dcf774385a7f8d9a771..18ae51530a38346261803be5c814e0b1383dbd14 100755 (executable)
@@ -5,11 +5,9 @@ import os
 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
+from treecutter.image import Image
 
 from treecutter import constants as const
 
@@ -18,59 +16,38 @@ class PhotoAlbum(object):
         self.uri = unicode(uri)
         self.albumid = abs(hash(self.uri))
         self.filelist = []
-        self.filelist_thumb = []
-        self.filelist_slider = []
-
-    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):
         d = self.uri
         for root, subdir, files in os.walk(d):
             for f in files:
-                self.filelist.append(os.path.join(root,f))
+                img = Image(os.path.join(root,f))
+                if not img.generated():
+                    self.filelist.append(img)
 
     def db_xml(self):
         db = ElementMaker(namespace=const.DB_NS, nsmap=const.NSMAP)
         sl = db.itemizedlist(**{const.XML+"id": "slider"})
         cnt = 0
-        for fs, f in zip(self.filelist_slider, self.filelist):
+        for img in 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()
+            caption = db.caption()
+            for p in img.caption().split('\n\n'):
+                caption.append(db.para(p))
+            link = db.para(db.link(img.infostr(),
+                                   **{const.XLINK+"href": img.filename()}))
+            caption.append(link)
             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)}))
+                        db.imageobject(db.imagedata(fileref=img.slider())),caption),
+                            **{const.XML+"id": "p%x%d" % (self.albumid,cnt)}))
 
         th = db.itemizedlist(**{const.XML+"id": "thumb"})
         cnt = 0
-        for f in self.filelist_thumb:
+        for img in self.filelist:
             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)}))))
+            th.append(db.listitem(db.para(db.link(db.inlinemediaobject(
+                        db.imageobject(db.imagedata(fileref=img.thumbnail()))),**{const.XLINK+"href": "#p%x%d" % (self.albumid, cnt)}))))
         return db.informalfigure(sl,th,**{const.XML+"id": "box"})
 
 def recursively_empty(e):
@@ -95,7 +72,6 @@ if __name__ == "__main__":
 
     album = PhotoAlbum(argument)
     album.files()
-    album.format()
     axml = album.db_xml()
 #    clean_db(axml)