Пример #1
0
    def save_bitmap_data(self, _zip):
        """
        Will save all Image tools to disk as temporary files, and then removes
        them. This function is lengthy because it will not save two idential
        images twice.
        """
        logger.debug("Writing bitmap files to zip")
        data = {}  # list of bitmap data, check if image has been pasted
        to_remove = []
        for canvas in self.gui.get_canvases():
            for shape in canvas.shapes:
                if isinstance(shape, tools.Image):
                    img = shape.image.ConvertToImage()
                    img_data = img.GetData()

                    for key, value in data.items():
                        if value == img_data:
                            if not shape.filename:
                                shape.filename = key
                            break

                    #  the above iteration didn't find any common pastes
                    if not shape.filename:
                        tmp_name = make_filename() + u".png"
                        img.SaveFile(tmp_name, wx.BITMAP_TYPE_PNG)
                        img = wx.Image(tmp_name)

                        name = make_filename() + u".jpg"
                        img.SaveFile(name, wx.BITMAP_TYPE_JPEG)
                        shape.filename = name
                        data[shape.filename] = img_data
                        _zip.write(name, os.path.join(u"data", name))
                        to_remove.append(name)
                        to_remove.append(tmp_name)

                    else:
                        name = shape.filename

                        if not name in to_remove:
                            data[name] = img_data
                            img.SaveFile(name, get_wx_image_type(name))
                            _zip.write(name, os.path.join("data", name))

        [os.remove(x) for x in to_remove]
Пример #2
0
    def convert(self):
        """
        If the filetype is PDF/PS, convert to a (temporary) series of images and
        loads them. Find out the directory length before/after the conversion to
        know how many 'pages' were converted - used then to create a new
        Whyteboard tabs for each page. The PDF's file location, convert quality
        and converted images are written into a "library" file, effectively
        caching the conversion.

        An attempt at randomising the temp. file name is made using alphanumeric
        characters to help minimise conflict.
        """
        if not self.im_location:
            self.prompt_for_im()

        if not self.im_location:  # above will have changed this if IM exists
            return

        _file = self.temp_file
        logger.info("Converting [%s]", os.path.basename(_file))

        quality = Config().convert_quality()
        cached = self.library.lookup(_file, quality)
        if cached:
            logger.debug("PDF is cached")
            self.display_converted(_file, cached)
        else:
            path = get_home_dir(u"wtbd-tmp")  # directory to store the images
            tmp_file = make_filename()
            before = os.walk(path).next()[2]  # file count before convert
            full_path = path + tmp_file + u".png"
            logger.debug("Writing PDF images as [%s]", full_path)

            cmd = convert_quality(quality, self.im_location, _file, full_path)
            logger.info("Starting to convert PDF")
            self.gui.convert_dialog(cmd)  # show progress bar, kick off convert

            if self.gui.convert_cancelled:
                logger.info("Convert process cancelled by user")
                return
            after = os.walk(path).next()[2]
            count = len(after) - len(before)
            images = []
            ignore = False
            logger.info("Convert process complete. %i images were created", count)

            if not count:
                logger.warning("Failed to convert.")
                wx.MessageBox(
                    _("Failed to convert file. Ensure GhostScript is installed\nhttp://pages.cs.wisc.edu/~ghost/"),
                    _("Conversion Failed"),
                )
                open_url(u"http://pages.cs.wisc.edu/~ghost/")
                return

            if count == 1:
                images.append(path + tmp_file + u".png")
                ignore = True
            else:
                for x in range(count):
                    # store the temp file path for this file in the dictionary
                    images.append(u"%s%s-%i.png" % (path, tmp_file, x))

            self.display_converted(_file, images, ignore)
            self.library.write(_file, images, quality)

        # Just in case it's a file with many pages
        self.gui.show_progress_dialog(_("Loading..."))
        self.gui.on_done_load()