From b0e371c695dbb52257e20bf49ab13da9953a8eee Mon Sep 17 00:00:00 2001 From: Don Allingham Date: Tue, 12 Jun 2001 17:02:19 +0000 Subject: [PATCH] image support svn: r113 --- src/HtmlDoc.py | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/src/HtmlDoc.py b/src/HtmlDoc.py index 23e50f990..8f4861ee4 100644 --- a/src/HtmlDoc.py +++ b/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('