def import_image(self, filename): """import image into report. The image is hard-linked. returns path to image for use in html. """ mangled_filename = re.sub("/", "_", filename) filename = os.path.abspath(filename) # directory in which to store images and thumbnails outdir = Utils.getOutputDirectory() image_filename = os.path.join(outdir, mangled_filename) # filenames to use in html - must take document hierarchy # into account rst2srcdir = os.path.join(os.path.relpath(self.src_dir, start=self.rst_dir), outdir) html_image_filename = os.path.join(rst2srcdir, mangled_filename) try: os.link(filename, image_filename) except OSError: # file exists pass return html_image_filename
def import_thumbnail(self, filename, thumbnail_size): '''import image in *filename* as a thumbnail. thumbnail_size is a tuple of (height, width) of the thumbnail in pixels. ''' mangled_filename = re.sub("/", "_", filename) # directory in which to store images and thumbnails outdir = Utils.getOutputDirectory() thumb_filename = os.path.join(outdir, "thumb-%s.png" % mangled_filename) image = PIL.Image.open(filename) image.thumbnail(thumbnail_size) image.save(thumb_filename) # filenames to use in html - must take document hierarchy # into account rst2srcdir = os.path.join( os.path.relpath(self.src_dir, start=self.rst_dir), outdir) html_thumb_filename = os.path.join(rst2srcdir, "thumb-%s.png" % mangled_filename) if self.build_environment: self.build_environment.dlfiles.add_file(self.rst_dir, html_thumb_filename) return html_thumb_filename