Exemple #1
0
    def test_write_bitmap(self):
        image = np.zeros([16, 16, 4], dtype="uint8")
        xs = np.random.rand(100)
        ys = np.random.rand(100)
        image = splat_points(image, xs, ys)
        png_str = write_bitmap(image.copy(), None)

        image_trans = image.swapaxes(0, 1).copy(order="C")
        png_str_trans = write_bitmap(image_trans, None, transpose=True)
        assert_equal(png_str, png_str_trans)

        with assert_raises(RuntimeError) as ex:
            write_bitmap(np.ones([16, 16]), None)
        desired = "Expecting image array of shape (N,M,3) or (N,M,4), received (16, 16)"
        assert_equal(str(ex.exception)[:50], desired[:50])
Exemple #2
0
def enhance(input_file, output_file, stdval=6.0, just_alpha=False):
    if just_alpha:
        im = pl.imread(input_file)[:,:,:3]
        nim = na.zeros_like(im)
        nz = im[im>0.0]
        nim = im/(nz.mean()+stdval*na.std(nz))
        nim[nim>1.0]=1.0
        nim[nim<0.0]=0.0
        write_bitmap(nim, output_file)
        del im, nim, nz
    else:
        im = pl.imread(input_file)[:,:,:3]
        nim = na.zeros_like(im)
        for c in range(3):
            nz = im[:,:,c][im[:,:,c]>0.0]
            nim[:,:,c] = im[:,:,c]/(nz.mean()+stdval*na.std(nz))
            del nz
        nim[:,:][nim>1.0]=1.0
        nim[:,:][nim<0.0]=0.0
        write_bitmap(nim, output_file)
        del im, nim
Exemple #3
0
 def render(self, scene):
     imgui.new_frame()
     changed = False
     if scene is not None:
         imgui.style_colors_classic()
         imgui.begin("Scene")
         imgui.text("Filename Template:")
         _, self.snapshot_format = imgui.input_text("",
                                                    self.snapshot_format,
                                                    256)
         if imgui.button("Save Snapshot"):
             # Call render again, since we're in the middle of overlaying
             # some stuff and we want a clean scene snapshot
             scene.render()
             write_bitmap(
                 scene.image[:, :, :3],
                 self.snapshot_format.format(count=self.snapshot_count),
             )
             self.snapshot_count += 1
         if imgui.tree_node("Debug"):
             if imgui.button("Save Depth"):
                 scene.render()
                 write_image(
                     scene.depth,
                     self.snapshot_format.format(count=self.snapshot_count),
                 )
                 self.snapshot_count += 1
             imgui.tree_pop()
         _ = self.render_camera(scene)
         changed = changed or _
         # imgui.show_style_editor()
         for i, element in enumerate(scene):
             if imgui.tree_node(f"element {i + 1}: {element.name}"):
                 changed = changed or element.render_gui(
                     imgui, self.renderer, scene)
                 imgui.tree_pop()
         self.window._do_update = self.window._do_update or changed
         imgui.end()
     imgui.render()
     self.renderer.render(imgui.get_draw_data())
Exemple #4
0
    def write_png(self,
                  filename,
                  sigma_clip=None,
                  background='black',
                  rescale=True,
                  clip_ratio=None):
        r"""Writes ImageArray to png file.

        Parameters
        ----------
        filename: string
            Filename to save to.  If None, PNG contents will be returned as a
            string.
        sigma_clip: float, optional
            Image will be clipped before saving to the standard deviation
            of the image multiplied by this value.  Useful for enhancing
            images. Default: None
        background:
            This can be used to set a background color for the image, and can
            take several types of values:

               * ``white``: white background, opaque
               * ``black``: black background, opaque
               * ``None``: transparent background
               * 4-element array [r,g,b,a]: arbitrary rgba setting.

            Default: 'black'
        rescale: boolean, optional
            If True, will write out a rescaled image (without modifying the
            original image). Default: True

        Examples
        --------
        >>> im = np.zeros([64,128,4])
        >>> for i in range(im.shape[0]):
        ...     for k in range(im.shape[2]):
        ...         im[i,:,k] = np.linspace(0.,10.*k, im.shape[1])

        >>> im_arr = ImageArray(im)
        >>> im_arr.write_png('standard.png')
        >>> im_arr.write_png('non-scaled.png', rescale=False)
        >>> im_arr.write_png('black_bg.png', background='black')
        >>> im_arr.write_png('white_bg.png', background='white')
        >>> im_arr.write_png('green_bg.png', background=[0,1,0,1])
        >>> im_arr.write_png('transparent_bg.png', background=None)

        """
        if rescale:
            scaled = self.rescale(inline=False)
        else:
            scaled = self

        if self.shape[-1] == 4:
            out = scaled.add_background_color(background, inline=False)
        else:
            out = scaled

        if filename is not None and filename[-4:] != '.png':
            filename += '.png'

        if clip_ratio is not None:
            warnings.warn(
                "'clip_ratio' keyword is deprecated. Use 'sigma_clip' instead")
            sigma_clip = clip_ratio

        if sigma_clip is not None:
            nz = out[:, :, :3][out[:, :, :3].nonzero()]
            return write_bitmap(out.swapaxes(0, 1), filename,
                                nz.mean() + sigma_clip * nz.std())
        else:
            return write_bitmap(out.swapaxes(0, 1), filename)
Exemple #5
0
    def write_png(
        self,
        filename,
        sigma_clip=None,
        background="black",
        rescale=True,
        clip_ratio=None,
    ):
        r"""Writes ImageArray to png file.

        Parameters
        ----------
        filename : string
            Filename to save to.  If None, PNG contents will be returned as a
            string.

        sigma_clip : float, optional
            Image will be clipped before saving to the standard deviation
            of the image multiplied by this value.  Useful for enhancing
            images. Default: None
        background:
            This can be used to set a background color for the image, and can
            take several types of values:

               * ``white``: white background, opaque
               * ``black``: black background, opaque
               * ``None``: transparent background
               * 4-element array [r,g,b,a]: arbitrary rgba setting.

            Default: 'black'

        rescale : boolean, optional
            If True, will write out a rescaled image (without modifying the
            original image). Default: True

        Examples
        --------
        >>> im = np.zeros([64, 128, 4])
        >>> for i in range(im.shape[0]):
        ...     for k in range(im.shape[2]):
        ...         im[i, :, k] = np.linspace(0.0, 10.0 * k, im.shape[1])

        >>> im_arr = ImageArray(im)
        >>> im_arr.write_png("standard.png")
        >>> im_arr.write_png("non-scaled.png", rescale=False)
        >>> im_arr.write_png("black_bg.png", background="black")
        >>> im_arr.write_png("white_bg.png", background="white")
        >>> im_arr.write_png("green_bg.png", background=[0, 1, 0, 1])
        >>> im_arr.write_png("transparent_bg.png", background=None)

        """
        if rescale:
            scaled = self.rescale(inline=False)
        else:
            scaled = self

        if self.shape[-1] == 4:
            out = scaled.add_background_color(background, inline=False)
        else:
            out = scaled

        if filename is not None and filename[-4:] != ".png":
            filename += ".png"

        if clip_ratio is not None:
            issue_deprecation_warning(
                "The 'clip_ratio' keyword argument is a deprecated alias for 'sigma_clip'. "
                "Please use 'sigma_clip' directly.",
                since="3.3",
                removal="4.2",
            )
            sigma_clip = clip_ratio

        if sigma_clip is not None:
            clip_value = self._clipping_value(sigma_clip, im=out)
            return write_bitmap(out.swapaxes(0, 1), filename, clip_value)
        else:
            return write_bitmap(out.swapaxes(0, 1), filename)
    def write_png(self, filename, clip_ratio=None, background='black',
                  rescale=True):
        r"""Writes ImageArray to png file.

        Parameters
        ----------
        filename: string
            Note filename not be modified.
        clip_ratio: float, optional
            Image will be clipped before saving to the standard deviation
            of the image multiplied by this value.  Useful for enhancing
            images. Default: None
        background:
            This can be used to set a background color for the image, and can
            take several types of values:

               * ``white``: white background, opaque
               * ``black``: black background, opaque
               * ``None``: transparent background
               * 4-element array [r,g,b,a]: arbitrary rgba setting.

            Default: 'black'
        rescale: boolean, optional
            If True, will write out a rescaled image (without modifying the
            original image). Default: True

        Examples
        --------
        >>> im = np.zeros([64,128,4])
        >>> for i in range(im.shape[0]):
        ...     for k in range(im.shape[2]):
        ...         im[i,:,k] = np.linspace(0.,10.*k, im.shape[1])

        >>> im_arr = ImageArray(im)
        >>> im_arr.write_png('standard.png')
        >>> im_arr.write_png('non-scaled.png', rescale=False)
        >>> im_arr.write_png('black_bg.png', background='black')
        >>> im_arr.write_png('white_bg.png', background='white')
        >>> im_arr.write_png('green_bg.png', background=[0,1,0,1])
        >>> im_arr.write_png('transparent_bg.png', background=None)

        """
        if rescale:
            scaled = self.rescale(inline=False)
        else:
            scaled = self

        if self.shape[-1] == 4:
            out = scaled.add_background_color(background, inline=False)
        else:
            out = scaled

        if filename[-4:] != '.png':
            filename += '.png'

        if clip_ratio is not None:
            nz = out[:, :, :3][out[:, :, :3].nonzero()]
            return write_bitmap(out.swapaxes(0, 1), filename,
                                nz.mean() + clip_ratio*nz.std())
        else:
            return write_bitmap(out.swapaxes(0, 1), filename)