diff --git a/gramps/src/HtmlDoc.py b/gramps/src/HtmlDoc.py index 23e50f990..8f4861ee4 100644 --- a/gramps/src/HtmlDoc.py +++ b/gramps/src/HtmlDoc.py @@ -30,6 +30,22 @@ _ = intl.gettext from TextDoc import * import const +#------------------------------------------------------------------------ +# +# Attempt to load the Python Imaging Library for the handling of photos. +# +#------------------------------------------------------------------------ +try: + import PIL.Image + no_pil = 0 +except: + no_pil = 1 + +#------------------------------------------------------------------------ +# +# Default template +# +#------------------------------------------------------------------------ _top = [ '\n', '\n', @@ -64,6 +80,7 @@ class HtmlDoc(TextDoc): self.template = template self.top = [] self.bottom = [] + self.base = "" def open(self,filename): @@ -120,6 +137,8 @@ class HtmlDoc(TextDoc): else: self.filename = filename + ".html" + self.base = os.path.dirname(self.filename) + self.f = open(self.filename,"w") for line in self.top: @@ -191,6 +210,43 @@ class HtmlDoc(TextDoc): self.f.write(line) self.f.close() + def add_photo(self,name,x,y): + if no_pil: + return + + im = PIL.Image.open(name) + nx,ny = im.size + + scale = float(nx)/float(ny) + if scale > 1.0: + scale = 1.0/scale + act_width = x + act_height = y * scale + else: + act_width = x * scale + act_height = y + + cmtopt = 72.0/2.54 + pixw = int(act_width*cmtopt) + pixx = int(act_height*cmtopt) + im.thumbnail((pixw,pixx)) + + imdir = self.base + os.sep + "images" + if not os.path.isdir(imdir): + try: + os.mkdir(imdir) + except: + return + + refname = "is%s" % os.path.basename(name) + try: + im.save(imdir + os.sep + refname) + except: + return + + self.f.write('\n' % \ + (refname,pixw,pixx)) + def start_table(self,name,style): self.tbl = self.table_styles[style] self.f.write('