Exemplo n.º 1
0
    def build_jpeg_preview(
        self,
        file_path: str,
        preview_name: str,
        cache_path: str,
        page_id: int,
        extension: str = ".jpg",
        size: ImgDims = None,
        mimetype: str = "",
    ) -> None:
        if not size:
            size = self.default_size
        with tempfile.TemporaryDirectory(
                prefix="preview-generator-") as tmp_dir:
            with open(file_path, "rb") as filestream:
                zip = zipfile.ZipFile(filestream)
                zip.extract("previews/preview.png", tmp_dir)
                zip.close()

                ImagePreviewBuilderPillow().build_jpeg_preview(
                    tmp_dir + "/previews/preview.png",
                    preview_name,
                    cache_path,
                    page_id,
                    extension,
                    size,
                    mimetype,
                )
Exemplo n.º 2
0
    def build_jpeg_preview(self,
                           file_path,
                           preview_name,
                           cache_path,
                           page_id,
                           extension='.jpg',
                           size=None):
        # inkscape tesselation-P3.svg  -e

        import tempfile
        import uuid

        tempfolder = tempfile.tempdir
        tmp_filename = '{}.png'.format(str(uuid.uuid4()))
        tmp_filepath = os.path.join(tempfolder, tmp_filename)
        build_png_result_code = check_call([
            'inkscape', file_path, '--export-area-drawing', '-e', tmp_filepath
        ],
                                           stdout=DEVNULL,
                                           stderr=STDOUT)

        if build_png_result_code != 0:
            raise PreviewGeneratorException(
                'Building PNG intermediate file using inkscape '
                'failed with status {}'.format(build_png_result_code))

        return ImagePreviewBuilderPillow().build_jpeg_preview(
            tmp_filepath, preview_name, cache_path, page_id, extension, size)
Exemplo n.º 3
0
    def build_jpeg_preview(
        self,
        file_path: str,
        preview_name: str,
        cache_path: str,
        page_id: int,
        extension: str = ".jpg",
        size: ImgDims = None,
        mimetype: str = "",
    ) -> None:
        if not size:
            size = self.default_size
        # inkscape tesselation-P3.svg  -e
        with tempfile.NamedTemporaryFile("w+b",
                                         prefix="preview-generator-",
                                         suffix=".png") as tmp_png:
            build_png_result_code = check_call(
                [
                    "inkscape", file_path, "--export-area-drawing", "-e",
                    tmp_png.name
                ],
                stdout=DEVNULL,
                stderr=STDOUT,
            )

            if build_png_result_code != 0:
                raise IntermediateFileBuildingFailed(
                    "Building PNG intermediate file using inkscape "
                    "failed with status {}".format(build_png_result_code))

            return ImagePreviewBuilderPillow().build_jpeg_preview(
                tmp_png.name, preview_name, cache_path, page_id, extension,
                size, mimetype)
Exemplo n.º 4
0
    def build_jpeg_preview(
        self,
        file_path: str,
        preview_name: str,
        cache_path: str,
        page_id: int,
        extension: str = ".jpg",
        size: ImgDims = None,
        mimetype: str = "",
    ) -> None:
        if not size:
            size = self.default_size
        # inkscape tesselation-P3.svg  -e
        with tempfile.NamedTemporaryFile("w+b",
                                         prefix="preview-generator-",
                                         suffix=".png") as tmp_png:
            build_png_result_code = self._imagemagick_convert(
                source_path=file_path,
                dest_path=tmp_png.name,
                mimetype=mimetype)

            if build_png_result_code != 0:
                raise IntermediateFileBuildingFailed(
                    "Building PNG intermediate file using convert "
                    "failed with status {}".format(build_png_result_code))

            return ImagePreviewBuilderPillow().build_jpeg_preview(
                tmp_png.name, preview_name, cache_path, page_id, extension,
                size)
Exemplo n.º 5
0
    def build_jpeg_preview(self,
                           file_path: str,
                           preview_name: str,
                           cache_path: str,
                           page_id: int,
                           extension: str = '.jpg',
                           size: ImgDims = None,
                           mimetype: str = '') -> None:
        # inkscape tesselation-P3.svg  -e
        tempfolder = tempfile.tempdir
        tmp_filename = '{}.png'.format(str(uuid.uuid4()))
        tmp_filepath = os.path.join(tempfolder, tmp_filename)
        build_png_result_code = check_call([
            'inkscape', file_path, '--export-area-drawing', '-e', tmp_filepath
        ],
                                           stdout=DEVNULL,
                                           stderr=STDOUT)

        if build_png_result_code != 0:
            raise IntermediateFileBuildingFailed(
                'Building PNG intermediate file using inkscape '
                'failed with status {}'.format(build_png_result_code))

        return ImagePreviewBuilderPillow().build_jpeg_preview(
            tmp_filepath, preview_name, cache_path, page_id, extension, size,
            mimetype)
Exemplo n.º 6
0
    def build_jpeg_preview(
        self,
        file_path: str,
        preview_name: str,
        cache_path: str,
        page_id: int,
        extension: str = ".jpg",
        size: ImgDims = None,
        mimetype: str = "",
    ) -> None:
        if not size:
            size = self.default_size
        # inkscape tesselation-P3.svg  -e
        tmp_filename = "{}.png".format(str(uuid.uuid4()))
        if tempfile.tempdir:
            tmp_filepath = os.path.join(tempfile.tempdir, tmp_filename)
        else:
            tmp_filepath = tmp_filename
        build_png_result_code = check_call(
            ["convert", file_path, "-layers", "merge", tmp_filepath], stdout=DEVNULL, stderr=STDOUT
        )

        if build_png_result_code != 0:
            raise IntermediateFileBuildingFailed(
                "Building PNG intermediate file using convert "
                "failed with status {}".format(build_png_result_code)
            )

        return ImagePreviewBuilderPillow().build_jpeg_preview(
            tmp_filepath, preview_name, cache_path, page_id, extension, size
        )
Exemplo n.º 7
0
    def build_jpeg_preview(
        self,
        file_path: str,
        preview_name: str,
        cache_path: str,
        page_id: int,
        extension: str = ".jpg",
        size: utils.ImgDims = None,
        mimetype: str = "",
    ) -> None:
        """
        generate the pdf small preview
        """
        # INFO - G.M - 2021-10-21 - Page id in pdftocairo begins at 1 instead of 0
        page_id = page_id + 1
        if not size:
            size = self.default_size

        with tempfile.NamedTemporaryFile("w+b",
                                         prefix="preview-generator-",
                                         suffix=".png") as tmp_png:
            build_png_result_code = check_call(
                [
                    PDFTOCAIRO_EXECUTABLE,
                    "-png",
                    "-singlefile",
                    "-scale-to",
                    str(size.max_dim()),
                    "-f",
                    str(page_id),
                    "-l",
                    str(page_id),
                    file_path,
                    # HACK - G.M - 2021-10-21 - For unclear reason, pdftocairo add a second .png
                    # extension to the file created.
                    tmp_png.name.rsplit(".png", 1)[0],
                ],
                stdout=DEVNULL,
                stderr=STDOUT,
            )
            if build_png_result_code != 0:
                raise IntermediateFileBuildingFailed(
                    "Building PNG intermediate file using pdftocairo failed with status {}"
                    .format(build_png_result_code))
            return ImagePreviewBuilderPillow().build_jpeg_preview(
                tmp_png.name, preview_name, cache_path, page_id, extension,
                size, mimetype)
    def build_jpeg_preview(
        self,
        file_path: str,
        preview_name: str,
        cache_path: str,
        page_id: int,
        extension: str = ".jpg",
        size: ImgDims = None,
        mimetype: str = "",
    ) -> None:
        if not size:
            size = self.default_size
        with tempfile.NamedTemporaryFile("w+b",
                                         prefix="preview-generator-",
                                         suffix=".jpg") as tmp_jpg:
            with Xvfb():
                build_jpg_result_code = check_call(
                    [
                        "/usr/bin/drawio",
                        "--no-sandbox",
                        "-x",
                        "-f",
                        "jpg",
                        "-o",
                        tmp_jpg.name,
                        file_path,
                    ],
                    stdout=DEVNULL,
                    stderr=STDOUT,
                    timeout=30,
                )

            if build_jpg_result_code != 0:
                raise IntermediateFileBuildingFailed(
                    "Building JPG intermediate file using drawio "
                    "failed with status {}".format(build_jpg_result_code))

            ImagePreviewBuilderPillow().build_jpeg_preview(
                tmp_jpg.name,
                preview_name,
                cache_path,
                page_id,
                extension,
                size,
                mimetype,
            )
    def build_jpeg_preview(
        self,
        file_path: str,
        preview_name: str,
        cache_path: str,
        page_id: int,
        extension: str = ".jpg",
        size: ImgDims = None,
        mimetype: str = "",
    ) -> None:

        if not size:
            size = self.default_size

        with tempfile.NamedTemporaryFile("w+b",
                                         prefix="preview-generator",
                                         suffix="png") as tmp_png:
            cairosvg.svg2png(url=file_path, write_to=tmp_png.name, dpi=96)

            return ImagePreviewBuilderPillow().build_jpeg_preview(
                tmp_png.name, preview_name, cache_path, page_id, extension,
                size, mimetype)
Exemplo n.º 10
0
    def build_jpeg_preview(
        self,
        file_path: str,
        preview_name: str,
        cache_path: str,
        page_id: int,
        extension: str = ".jpg",
        size: ImgDims = None,
        mimetype: str = "",
    ) -> None:
        if not size:
            size = self.default_size

        tmp_filename = "{}.png".format(str(uuid.uuid4()))
        if tempfile.tempdir:
            tmp_filepath = os.path.join(tempfile.tempdir, tmp_filename)
        else:
            tmp_filepath = tmp_filename

        colors = vtkNamedColors()

        reader = vtkSTLReader()  # TODO analyse wich file format is use
        reader.SetFileName(file_path)

        mapper = vtkPolyDataMapper()
        mapper.SetInputConnection(reader.GetOutputPort())

        actor = vtkActor()
        actor.SetMapper(mapper)

        rotation = (-70, 0, 45)
        R_x, R_y, R_z = rotation  # TODO set a good looking default orientation
        actor.RotateX(R_x)
        actor.RotateY(R_y)
        actor.RotateZ(R_z)

        # Create a rendering window and renderer
        ren = vtkRenderer()
        renWin = vtkRenderWindow()
        renWin.OffScreenRenderingOn()
        renWin.AddRenderer(ren)
        ren.SetBackground(colors.GetColor3d("white"))

        # Assign actor to the renderer
        ren.AddActor(actor)

        renWin.Render()

        # Write image
        windowto_image_filter = vtkWindowToImageFilter()
        windowto_image_filter.SetInput(renWin)
        # windowto_image_filter.SetScale(scale)  # image scale
        windowto_image_filter.SetInputBufferTypeToRGBA()
        writer = vtkPNGWriter()
        writer.SetFileName(tmp_filepath)
        writer.SetInputConnection(windowto_image_filter.GetOutputPort())
        writer.Write()

        return ImagePreviewBuilderPillow().build_jpeg_preview(
            tmp_filepath, preview_name, cache_path, page_id, extension, size,
            mimetype)
Exemplo n.º 11
0
    def build_jpeg_preview(
        self,
        file_path: str,
        preview_name: str,
        cache_path: str,
        page_id: int,
        extension: str = ".jpg",
        size: ImgDims = None,
        mimetype: str = "",
    ) -> None:
        if not size:
            size = self.default_size

        colors = vtkNamedColors()

        if not mimetype:
            guessed_mimetype, _ = mimetypes_storage.guess_type(file_path,
                                                               strict=False)
            # INFO - G.M - 2019-11-22 - guessed_mimetype can be None
            mimetype = guessed_mimetype or ""
        reader = self._get_vtk_reader(mimetype)
        reader.SetFileName(file_path)

        mapper = vtkPolyDataMapper()
        mapper.SetInputConnection(reader.GetOutputPort())

        actor = vtkActor()
        actor.SetMapper(mapper)

        rotation = (-70, 0, 45)
        R_x, R_y, R_z = rotation  # TODO set a good looking default orientation
        actor.RotateX(R_x)
        actor.RotateY(R_y)
        actor.RotateZ(R_z)

        # Create a rendering window and renderer
        ren = vtkRenderer()
        renWin = vtkRenderWindow()
        renWin.OffScreenRenderingOn()
        renWin.AddRenderer(ren)
        renWin.SetSize(size.width, size.height)
        ren.SetBackground(colors.GetColor3d("white"))

        # Assign actor to the renderer
        ren.AddActor(actor)

        renWin.Render()

        # Write image
        windowto_image_filter = vtkWindowToImageFilter()
        windowto_image_filter.SetInput(renWin)
        # windowto_image_filter.SetScale(scale)  # image scale
        windowto_image_filter.SetInputBufferTypeToRGBA()

        with tempfile.NamedTemporaryFile("w+b",
                                         prefix="preview-generator-",
                                         suffix=".png") as tmp_png:
            writer = vtkPNGWriter()
            writer.SetFileName(tmp_png.name)
            writer.SetInputConnection(windowto_image_filter.GetOutputPort())
            writer.Write()

            return ImagePreviewBuilderPillow().build_jpeg_preview(
                tmp_png.name, preview_name, cache_path, page_id, extension,
                size, mimetype)