Ejemplo n.º 1
0
    def write_image(self, filename, color_bounds=None, channel=None,
                    cmap_name=None, func=lambda x: x):
        r"""Writes a single channel of the ImageArray to a png file.

        Parameters
        ----------
        filename: string
            Note filename not be modified.

        Other Parameters
        ----------------
        channel: int
            Which channel to write out as an image. Defaults to 0
        cmap_name: string
            Name of the colormap to be used.
        color_bounds : tuple of floats, optional
            The min and max to scale between.  Outlying values will be clipped.
        cmap_name : string, optional
            An acceptable colormap.  See either yt.visualization.color_maps or
            http://www.scipy.org/Cookbook/Matplotlib/Show_colormaps .
        func : function, optional
            A function to transform the buffer before applying a colormap.

        Returns
        -------
        scaled_image : uint8 image that has been saved

        Examples
        --------

        >>> im = np.zeros([64,128])
        >>> for i in range(im.shape[0]):
        ...     im[i,:] = np.linspace(0.,0.3*i, im.shape[1])

        >>> myinfo = {'field':'dinosaurs', 'east_vector':np.array([1.,0.,0.]),
        ...     'north_vector':np.array([0.,0.,1.]), 'normal_vector':np.array([0.,1.,0.]),
        ...     'width':0.245, 'units':'cm', 'type':'rendering'}

        >>> im_arr = ImageArray(im, info=myinfo)
        >>> im_arr.write_image('test_ImageArray.png')

        """
        if cmap_name is None:
            cmap_name = ytcfg.get("yt", "default_colormap")
        if filename[-4:] != '.png':
            filename += '.png'

        #TODO: Write info dict as png metadata
        if channel is None:
            return write_image(self.swapaxes(0, 1).to_ndarray(), filename,
                               color_bounds=color_bounds, cmap_name=cmap_name,
                               func=func)
        else:
            return write_image(self.swapaxes(0, 1)[:, :, channel].to_ndarray(),
                               filename,
                               color_bounds=color_bounds, cmap_name=cmap_name,
                               func=func)
    def write_image(self, filename, color_bounds=None, channel=None,
                    cmap_name="algae", func=lambda x: x):
        r"""Writes a single channel of the ImageArray to a png file.

        Parameters
        ----------
        filename: string
            Note filename not be modified.

        Other Parameters
        ----------------
        channel: int
            Which channel to write out as an image. Defaults to 0
        cmap_name: string
            Name of the colormap to be used.
        color_bounds : tuple of floats, optional
            The min and max to scale between.  Outlying values will be clipped.
        cmap_name : string, optional
            An acceptable colormap.  See either yt.visualization.color_maps or
            http://www.scipy.org/Cookbook/Matplotlib/Show_colormaps .
        func : function, optional
            A function to transform the buffer before applying a colormap.

        Returns
        -------
        scaled_image : uint8 image that has been saved

        Examples
        --------

        >>> im = np.zeros([64,128])
        >>> for i in range(im.shape[0]):
        ...     im[i,:] = np.linspace(0.,0.3*i, im.shape[1])

        >>> myinfo = {'field':'dinosaurs', 'east_vector':np.array([1.,0.,0.]),
        ...     'north_vector':np.array([0.,0.,1.]), 'normal_vector':np.array([0.,1.,0.]),
        ...     'width':0.245, 'units':'cm', 'type':'rendering'}

        >>> im_arr = ImageArray(im, info=myinfo)
        >>> im_arr.write_image('test_ImageArray.png')

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

        #TODO: Write info dict as png metadata
        if channel is None:
            return write_image(self.swapaxes(0, 1).to_ndarray(), filename,
                               color_bounds=color_bounds, cmap_name=cmap_name,
                               func=func)
        else:
            return write_image(self.swapaxes(0, 1)[:, :, channel].to_ndarray(),
                               filename,
                               color_bounds=color_bounds, cmap_name=cmap_name,
                               func=func)
Ejemplo n.º 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())
Ejemplo n.º 4
0
    grid = gs[0]  # main grid

    # create a frb: antialias must be True if you don't want your data altered
    prj = ds.proj('density', 2)
    prj_frb = FixedResolutionBuffer(prj, (xo, xf, yo, yf), (ypix, xpix),
                                    antialias=True,
                                    periodic=True)

    # load all specified variables
    for var in vars:
        new_prj = prj_frb[var]
        img = np.array(new_prj)
        if PNG_IMAGES:
            img_name = var + "%03d" % (i, )
            write_image(img,
                        imagefolder + img_name + '.png',
                        cmap_name='Spectral')
        else:
            img_name = var + ` i `
            varfile = open(imagefolder + img_name, 'w')
            np.save(varfile, img)
            #np.save(varfile,np.log10(img))
            varfile.close()
        print "Saving image {}...".format(img_name)

# save position coordinates (done just once since a fixed grid is assumed)
if not (PNG_IMAGES):
    x_prj = prj_frb['x']
    x_img = np.array(x_prj)
    y_prj = prj_frb['y']
    y_img = np.array(y_prj)
Ejemplo n.º 5
0
    def write_image(
        self,
        filename,
        color_bounds=None,
        channel=None,
        cmap_name=None,
        func=lambda x: x,
    ):
        r"""Writes a single channel of the ImageArray to a png file.

        Parameters
        ----------
        filename : string
            Note filename not be modified.

        Other Parameters
        ----------------
        channel: int
            Which channel to write out as an image. Defaults to 0
        cmap_name: string
            Name of the colormap to be used.
        color_bounds : tuple of floats, optional
            The min and max to scale between.  Outlying values will be clipped.
        cmap_name : string, optional
            An acceptable colormap.  See either yt.visualization.color_maps or
            https://scipy-cookbook.readthedocs.io/items/Matplotlib_Show_colormaps.html .
        func : function, optional
            A function to transform the buffer before applying a colormap.

        Returns
        -------
        scaled_image : uint8 image that has been saved

        Examples
        --------

        >>> im = np.zeros([64, 128])
        >>> for i in range(im.shape[0]):
        ...     im[i, :] = np.linspace(0.0, 0.3 * i, im.shape[1])

        >>> myinfo = {
        ...     "field": "dinosaurs",
        ...     "east_vector": np.array([1.0, 0.0, 0.0]),
        ...     "north_vector": np.array([0.0, 0.0, 1.0]),
        ...     "normal_vector": np.array([0.0, 1.0, 0.0]),
        ...     "width": 0.245,
        ...     "units": "cm",
        ...     "type": "rendering",
        ... }

        >>> im_arr = ImageArray(im, info=myinfo)
        >>> im_arr.write_image("test_ImageArray.png")

        """
        if cmap_name is None:
            cmap_name = ytcfg.get("yt", "default_colormap")
        if filename is not None and filename[-4:] != ".png":
            filename += ".png"

        # TODO: Write info dict as png metadata
        if channel is None:
            return write_image(
                self.swapaxes(0, 1).to_ndarray(),
                filename,
                color_bounds=color_bounds,
                cmap_name=cmap_name,
                func=func,
            )
        else:
            return write_image(
                self.swapaxes(0, 1)[:, :, channel].to_ndarray(),
                filename,
                color_bounds=color_bounds,
                cmap_name=cmap_name,
                func=func,
            )