xinclude: added pic and latests plugins
[treecutter.git] / xinclude / pic.py
diff --git a/xinclude/pic.py b/xinclude/pic.py
new file mode 100755 (executable)
index 0000000..3710450
--- /dev/null
@@ -0,0 +1,70 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
+from dateutil.tz import *
+from datetime import datetime
+from dateutil.parser import parse
+
+import os
+import sys
+import Image
+
+from lxml import etree
+from lxml.builder import ElementMaker
+
+from treecutter import constants as const
+
+class Pic(object):
+    def __init__(self, picture, caption):
+        self.picture = picture
+        self.caption = caption
+        self.sizes = [(500,500),(800,800),(1024,1024)]
+        self.outfiles = []
+
+    def thumbnails(self):
+        infile = os.path.splitext(self.picture)
+        for size in self.sizes:
+            outfile = infile[0]+"-"+str(size[0])+infile[1]
+            if infile != outfile:
+                try:
+                    im = Image.open(self.picture)
+                    im.thumbnail(size, Image.ANTIALIAS)
+                    im.save(outfile, im.format)
+                    self.outfiles.append(outfile)
+                except IOError:
+                    print "cannot create thumbnail for '%s'" % infile
+        self.outfiles.append(self.picture)
+
+    def db_xml(self):
+        webpic = self.outfiles[0]
+        imw = Image.open(webpic)
+        ww,dw = imw.size
+
+        db = ElementMaker(namespace=const.DB_NS, nsmap=const.NSMAP)
+
+        pics = db.para()
+        for f in self.outfiles:
+            im = Image.open(f)
+            w,d = im.size
+            pics.append(db.link(str(w)+"x"+str(d)+" ",**{const.XLINK+"href": f}))
+        #pics = list(', '.join(pics))
+        #pics.append(')')
+        #        pics.insert(0,'Sizes (')
+        lst = db.mediaobject(
+            db.imageobject(
+                db.imagedata(fileref=webpic, width=str(ww), depth=str(dw))),
+            db.caption(db.para(self.caption),pics))
+        return lst
+
+if __name__ == "__main__":
+    for arg in sys.argv[1:]:
+        al = arg.split("=")
+        if al[0] == "lang":
+            lang = al[1]
+        if al[0] == "xptr":
+            argument = al[1]
+    p,c = argument.split("|")
+    pic = Pic(p,c)
+    pic.thumbnails()
+    pxml = pic.db_xml()
+    sys.stdout.write(etree.tostring(pxml,encoding='UTF-8',pretty_print=False))