Пример #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()

                ImagePreviewBuilderWand().build_jpeg_preview(
                    tmp_dir + "/previews/preview.png",
                    preview_name,
                    cache_path,
                    page_id,
                    extension,
                    size,
                    mimetype,
                )
Пример #2
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(
                get_inkscape_parameters(file_path, 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 ImagePreviewBuilderWand().build_jpeg_preview(
                tmp_png.name, preview_name, cache_path, page_id, extension,
                size, mimetype)
Пример #3
0
def test_compute_resize_dims_right_limits():
    dims_in = ImgDims(520, 206)  # vertical
    dims_out = ImgDims(512, 256)  # horizontal

    builder = ImagePreviewBuilderWand()
    dims_resize = compute_resize_dims(dims_in, dims_out)
    assert dims_resize.width == 512
    assert dims_resize.height == 203
Пример #4
0
def test_compute_resize_dims_different_ratio_inverted():
    dims_in = ImgDims(198, 600)  # vertical
    dims_out = ImgDims(400, 100)  # horizontal

    builder = ImagePreviewBuilderWand()
    dims_resize = compute_resize_dims(dims_in, dims_out)
    assert dims_resize.width == 33
    assert dims_resize.height == 100
Пример #5
0
def test_compute_resize_dims_same_format():
    dims_in = ImgDims(100, 50)
    dims_out = ImgDims(90, 30)

    builder = ImagePreviewBuilderWand()
    dims_resize = compute_resize_dims(dims_in, dims_out)
    assert dims_resize.width == 60
    assert dims_resize.height == 30
Пример #6
0
def test_compute_resize_dims_same_ratio():
    dims_in = ImgDims(100, 50)
    dims_out = ImgDims(200, 100)

    builder = ImagePreviewBuilderWand()
    dims_resize = compute_resize_dims(dims_in, dims_out)
    assert dims_resize.width == 200
    assert dims_resize.height == 100
Пример #7
0
def test_build_jpeg_preview() -> None:
    wand_builder = ImagePreviewBuilderWand()
    test_orient_path = os.path.join(CURRENT_DIR, "the_img.png")
    extension = ".jpg"
    preview_name = "preview_the_img"
    width = 512
    height = 256
    size = ImgDims(width=width, height=height)
    wand_builder.build_jpeg_preview(
        file_path=test_orient_path,
        preview_name=preview_name,
        cache_path=CACHE_DIR,
        page_id=-1,
        size=size,
        extension=extension,
    )
    preview_name = preview_name + extension
    dest_path = os.path.join(CACHE_DIR, preview_name)
    assert os.path.exists(dest_path)
    assert os.path.getsize(dest_path) > 0
    with Image.open(dest_path) as jpg:
        assert jpg.height == height
        assert jpg.width in range(288, 290)
Пример #8
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.NamedTemporaryFile("w+b",
                                         prefix="preview-generator-",
                                         suffix=".jpg") as tmp_jpg:
            with Xvfb():
                build_jpg_result_code = check_call(
                    [
                        "drawio",
                        "-x",
                        "-f",
                        "jpg",
                        "-o",
                        tmp_jpg.name,
                        file_path,
                        # INFO - G.M - 12/11/2021 - Add no-sandbox at the end as putting it before
                        # doesn't work, see:
                        # https://github.com/jgraph/drawio-desktop/issues/249#issuecomment-695179747
                        "--no-sandbox",
                    ],
                    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))

            ImagePreviewBuilderWand().build_jpeg_preview(
                tmp_jpg.name, preview_name, cache_path, page_id, extension,
                size, mimetype)
Пример #9
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.NamedTemporaryFile("w+b",
                                         prefix="preview-generator",
                                         suffix="png") as tmp_png:
            cairosvg.svg2png(url=file_path, write_to=tmp_png.name, dpi=96)

            return ImagePreviewBuilderWand().build_jpeg_preview(
                tmp_png.name, preview_name, cache_path, page_id, extension,
                size, mimetype)
Пример #10
0
    def build_jpeg_preview(
        self,
        file_path: str,
        preview_name: str,
        cache_path: str,
        page_id: int,
        extension: str = ".jpeg",
        size: ImgDims = None,
        mimetype: str = "",
    ) -> None:
        if not size:
            size = self.default_size
        with tempfile.NamedTemporaryFile(
            "w+b", prefix="preview-generator", suffix=".tiff"
        ) as tmp_tiff:
            with rawpy.imread(file_path) as raw:
                processed_image = raw.postprocess(use_auto_wb=True)
                with Image.from_array(processed_image) as img:
                    img.save(filename=tmp_tiff.name)

            return ImagePreviewBuilderWand().build_jpeg_preview(
                tmp_tiff.name, preview_name, cache_path, page_id, extension, size, mimetype
            )
Пример #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)
        reader.Update()

        mapper = vtkPolyDataMapper()

        # set parent node as output for GLTF
        if mimetype == "model/gltf":
            mesh = reader.GetOutput()
            mesh_parent = mesh.GetDataSet(mesh.NewIterator())
            mapper.SetInputData(mesh_parent)
        else:
            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 ImagePreviewBuilderWand().build_jpeg_preview(
                tmp_png.name, preview_name, cache_path, page_id, extension, size, mimetype
            )