示例#1
0
    def set_image(self, img):
        import numpy as np

        import taichi as ti

        if self.fast_gui:
            assert isinstance(img, ti.Matrix), \
                    "Only ti.Vector.field is supported in GUI.set_image when fast_gui=True"
            assert img.shape == self.res, \
                    "Image resolution does not match GUI resolution"
            assert img.n in [3, 4] and img.m == 1, \
                    "Only RGB images are supported in GUI.set_image when fast_gui=True"
            assert img.dtype in [ti.f32, ti.f64, ti.u8], \
                    "Only f32, f64, u8 are supported in GUI.set_image when fast_gui=True"

            from taichi.lang.meta import vector_to_fast_image
            vector_to_fast_image(img, self.img)
            return

        if isinstance(img, ti.Expr):
            if _ti_core.is_integral(img.dtype) or len(img.shape) != 2:
                # Images of uint is not optimized by xxx_to_image
                self.img = self.cook_image(img.to_numpy())
            else:
                # Type matched! We can use an optimized copy kernel.
                assert img.shape \
                 == self.res, "Image resolution does not match GUI resolution"
                from taichi.lang.meta import tensor_to_image
                tensor_to_image(img, self.img)
                ti.sync()

        elif isinstance(img, ti.Matrix):
            if _ti_core.is_integral(img.dtype):
                self.img = self.cook_image(img.to_numpy())
            else:
                # Type matched! We can use an optimized copy kernel.
                assert img.shape  == self.res, \
                        "Image resolution does not match GUI resolution"
                assert img.n in [2, 3, 4] and img.m == 1, \
                        "Only greyscale, RG, RGB or RGBA images are supported in GUI.set_image"
                from taichi.lang.meta import vector_to_image
                vector_to_image(img, self.img)
                ti.sync()

        elif isinstance(img, np.ndarray):
            self.img = self.cook_image(img)

        else:
            raise ValueError(
                f"GUI.set_image only takes a Taichi field or NumPy array, not {type(img)}"
            )

        self.core.set_img(self.img.ctypes.data)
示例#2
0
    def set_image(self, img):
        import numpy as np
        import taichi as ti

        if isinstance(img, ti.Expr):
            if ti.core.is_integral(img.dtype) or len(img.shape) != 2:
                # Images of uint is not optimized by xxx_to_image
                self.img = self.cook_image(img.to_numpy())
            else:
                # Type matched! We can use an optimized copy kernel.
                assert img.shape \
                 == self.res, "Image resolution does not match GUI resolution"
                from taichi.lang.meta import tensor_to_image
                tensor_to_image(img, self.img)
                ti.sync()

        elif isinstance(img, ti.Matrix):
            if ti.core.is_integral(img.dtype):
                self.img = self.cook_image(img.to_numpy())
            else:
                # Type matched! We can use an optimized copy kernel.
                assert img.shape \
                 == self.res, "Image resolution does not match GUI resolution"
                assert img.n in [
                    3, 4
                ], "Only greyscale, RGB or RGBA images are supported in GUI.set_image"
                assert img.m == 1
                from taichi.lang.meta import vector_to_image
                vector_to_image(img, self.img)
                ti.sync()

        elif isinstance(img, np.ndarray):
            self.img = self.cook_image(img)

        else:
            raise ValueError(
                f"GUI.set_image only takes a Taichi tensor or NumPy array, not {type(img)}"
            )

        self.core.set_img(self.img.ctypes.data)
示例#3
0
    def set_image(self, img):
        """Draw an image on canvas.

        Args:
            img (Union[ti.field, numpy.array]): The color array representing the
                image to be drawn. Support greyscale, RG, RGB, and RGBA color
                representations. Its shape must match GUI resolution.

        """
        import numpy as np
        from taichi.lang.field import ScalarField
        from taichi.lang.matrix import MatrixField

        import taichi as ti

        if self.fast_gui:
            assert isinstance(img, MatrixField), \
                    "Only ti.Vector.field is supported in GUI.set_image when fast_gui=True"
            assert img.shape == self.res, \
                    "Image resolution does not match GUI resolution"
            assert img.n in [3, 4] and img.m == 1, \
                    "Only RGB images are supported in GUI.set_image when fast_gui=True"
            assert img.dtype in [ti.f32, ti.f64, ti.u8], \
                    "Only f32, f64, u8 are supported in GUI.set_image when fast_gui=True"

            from taichi.lang.meta import vector_to_fast_image
            vector_to_fast_image(img, self.img)
            return

        if isinstance(img, ScalarField):
            if _ti_core.is_integral(img.dtype) or len(img.shape) != 2:
                # Images of uint is not optimized by xxx_to_image
                self.img = self.cook_image(img.to_numpy())
            else:
                # Type matched! We can use an optimized copy kernel.
                assert img.shape \
                 == self.res, "Image resolution does not match GUI resolution"
                from taichi.lang.meta import tensor_to_image
                tensor_to_image(img, self.img)
                ti.sync()

        elif isinstance(img, MatrixField):
            if _ti_core.is_integral(img.dtype):
                self.img = self.cook_image(img.to_numpy())
            else:
                # Type matched! We can use an optimized copy kernel.
                assert img.shape  == self.res, \
                        "Image resolution does not match GUI resolution"
                assert img.n in [2, 3, 4] and img.m == 1, \
                        "Only greyscale, RG, RGB or RGBA images are supported in GUI.set_image"
                from taichi.lang.meta import vector_to_image
                vector_to_image(img, self.img)
                ti.sync()

        elif isinstance(img, np.ndarray):
            self.img = self.cook_image(img)

        else:
            raise ValueError(
                f"GUI.set_image only takes a Taichi field or NumPy array, not {type(img)}"
            )

        self.core.set_img(self.img.ctypes.data)