Exemplo n.º 1
0
    def hack_mlabsavefig(self, fname, size):
        oldx, oldy = self.scene.scene_editor.get_size()

        curx, cury = self.scene.scene_editor.control.Parent.Parent.Size
        cury -= 32  #the mayavi toolbar takes up 32 space, which is not what
        #we are setting

        self.scene.scene_editor.set_size((curx, cury))
        self.txt.visible = False  #label visibility is fixed in mayavi 4.3.1

        magnif_desired = max(size[0] // curx, size[1] // cury) + 1
        newsize = (int(size[0] / magnif_desired),
                   int(size[1] / magnif_desired))
        self.scene.scene_editor.set_size(newsize)
        from tvtk.api import tvtk
        filter = tvtk.WindowToImageFilter(read_front_buffer=True)
        filter.magnification = int(magnif_desired)
        self.scene.scene_editor._lift()
        filter.input = self.scene.scene_editor._renwin
        ex = tvtk.PNGWriter()
        ex.file_name = fname
        ex.input = filter.output
        self.scene.scene_editor._exporter_write(ex)

        #reset the old size
        self.scene.scene_editor.set_size((oldx, oldy))
        #restore the 3D region text if it was previously enabled
        self.txt.visible = self.ds.opts.show_floating_text
Exemplo n.º 2
0
 def save_png(self, file_name):
     """Save to a PNG image file."""
     if len(file_name) != 0:
         w2if = self._get_window_to_image()
         ex = tvtk.PNGWriter()
         ex.file_name = file_name
         configure_input(ex, w2if)
         self._exporter_write(ex)
Exemplo n.º 3
0
 def export_picture(self, filename):
     w2if = tvtk.WindowToImageFilter()
     w2if.magnification = 2
     w2if.input = self.render_window
     ex = tvtk.PNGWriter()
     ex.file_name = filename
     ex.input = w2if.output
     ex.write()
Exemplo n.º 4
0
def scene_to_png(scene):
    w2if = tvtk.WindowToImageFilter()
    w2if.input = scene.render_window
    ex = tvtk.PNGWriter()
    ex.write_to_memory = True
    configure_input(ex, w2if)
    ex.update()
    ex.write()
    data = base64.b64encode(ex.result.to_array()).decode('ascii')
    html = '<img src="data:image/png;base64,%s" alt="PNG image"></img>'
    return html % data
Exemplo n.º 5
0
 def save_png(self, file_name):
     """Save to a PNG image file."""
     if len(file_name) != 0:
         w2if = tvtk.WindowToImageFilter(read_front_buffer=True)
         w2if.magnification = self.magnification
         self._lift()
         w2if.input = self._renwin
         ex = tvtk.PNGWriter()
         ex.file_name = file_name
         ex.input = w2if.output
         self._exporter_write(ex)
Exemplo n.º 6
0
 def save_png(self, file_name):
     """Save to a PNG image file."""
     if len(file_name) != 0:
         w2if = tvtk.WindowToImageFilter(
             read_front_buffer=not self.off_screen_rendering)
         w2if.magnification = self.magnification
         self._lift()
         w2if.input = self._renwin
         ex = tvtk.PNGWriter()
         ex.file_name = file_name
         configure_input(ex, w2if)
         self._exporter_write(ex)
Exemplo n.º 7
0
 def on_key_down(iren_style, key):
     rendrr = iren_style.GetCurrentRenderer()
     rendrr = tvtk.to_tvtk(rendrr)
     if key == '+':
         for act in rendrr.actors:
             act.property.point_size *= 1.1
     elif key == '-':
         for act in rendrr.actors:
             act.property.point_size /= 1.1
     elif key == 's':
         ren_win = rendrr.render_window
         w2if = tvtk.WindowToImageFilter(input=ren_win)
         w2if.update()
         writer = tvtk.PNGWriter(file_name=snapshot_file, input=w2if.output)
         writer.write()
Exemplo n.º 8
0
def _handle_failed_image(idiff, src_img, pngr, img_fname):
    """Writes all the necessary images when an image comparison
    failed."""
    f_base, f_ext = os.path.splitext(img_fname)

    # write out the difference file in full.
    pngw = tvtk.PNGWriter(file_name=f_base + ".diff.png")
    configure_input(pngw, idiff)
    pngw.write()

    # write the difference image scaled and gamma adjusted for the
    # dashboard.
    sz = pngr.output.dimensions
    if sz[1] <= 250.0:
        mag = 1.0
    else:
        mag = 250.0/sz[1]

    shrink = tvtk.ImageResample(interpolate=1)
    configure_input(shrink, idiff.output)
    shrink.set_axis_magnification_factor(0, mag)
    shrink.set_axis_magnification_factor(1, mag)

    gamma = tvtk.ImageShiftScale(shift=0, scale=10)
    configure_input(gamma, shrink)

    jpegw = tvtk.JPEGWriter(file_name=f_base + ".diff.small.jpg",
                            quality=85)
    configure_input(jpegw, gamma)
    jpegw.write()

    # write out the image that was generated.
    pngw.trait_set(file_name=f_base + ".test.png")
    configure_input(pngw, src_img)
    pngw.write()

    # write out a smaller version of the image that was generated.
    configure_input(shrink, idiff.input)
    jpegw.trait_set(file_name=f_base + ".test.small.jpg")
    configure_input(jpegw, shrink)
    jpegw.write()

    # write out the valid image that matched.
    configure_input(shrink, idiff.image)
    jpegw.trait_set(file_name=f_base + ".small.jpg")
    configure_input(jpegw, shrink)
    jpegw.write()
Exemplo n.º 9
0
    def render_bitmap(self,
                      width,
                      height,
                      filename=None,
                      azimuth=15.0,
                      elevation=30.0,
                      roll=0.0,
                      zoom=1.0,
                      pan_h=0.0,
                      pan_v=0.0):
        renderer = tvtk.Renderer()
        for actor in self.scene.actor_list:
            renderer.add_actor(actor)
        renderer.background = (1, 1, 0.8)

        renderer.reset_camera()
        camera = renderer.active_camera
        camera.roll(roll)
        camera.elevation(elevation)
        camera.azimuth(azimuth)
        camera.dolly(zoom)
        camera.yaw(pan_h)
        camera.pitch(pan_v)

        renderWindow = tvtk.RenderWindow()
        renderWindow.off_screen_rendering = True
        renderWindow.add_renderer(renderer)
        renderWindow.size = (width, height)
        renderWindow.render()

        windowToImageFilter = tvtk.WindowToImageFilter()
        windowToImageFilter.input = renderWindow
        windowToImageFilter.update()

        writer = tvtk.PNGWriter()
        if filename is not None:
            writer.file_name = filename
            writer.write_to_memory = False
        else:
            writer.write_to_memory = True
        writer.input_connection = windowToImageFilter.output_port
        writer.write()
        #data = numpy.asarray(writer.result).tostring()
        return writer.result
Exemplo n.º 10
0
    def _render_animation_fired(self):
        self.stop = True
        n_frames_render = self.render_to_frame - self.render_from_frame
        # prepare the render window
        renwin = self._figure.scene.render_window
        aa_frames = renwin.aa_frames
        renwin.aa_frames = 8
        renwin.alpha_bit_planes = 1
        # turn on off screen rendering
        #renwin.off_screen_rendering = True
        # set size of window
        if self.fix_image_size:
            orig_size = renwin.size
            renwin.size = self.image_size
        # render the frames
        progress = ProgressDialog(title="Rendering", max=n_frames_render, 
                                  show_time=True, can_cancel=True)
        progress.open()
        self.is_rendering_animation = True
        for frame in range(self.render_from_frame, self.render_to_frame + 1):
            # move animation to desired frame, this will also render the scene
            self.current_frame = frame
            # prepare window to image writer
            render = tvtk.WindowToImageFilter(input=renwin, magnification=1)#, input_buffer_type='rgba')
            if not self.fix_image_size:
                render.magnification = self.magnification
            exporter = tvtk.PNGWriter(file_name=path.join(self.render_directory, self.render_name_pattern % frame))

            configure_input(exporter,render)
            exporter.write()
            do_continue, skip = progress.update(frame - self.render_from_frame)
            if not do_continue:
                break
        # reset the render window to old values
        renwin.aa_frames = aa_frames
        if self.fix_image_size:
            renwin.size = orig_size
        #renwin.off_screen_rendering = False
        self.is_rendering_animation = False
        progress.close()
Exemplo n.º 11
0
def compare_image_with_saved_image(src_img,
                                   img_fname,
                                   threshold=10,
                                   allow_resize=True):
    """Compares a source image (src_img, which is a tvtk.ImageData)
    with the saved image file whose name is given in the second
    argument.  If the image file does not exist the image is generated
    and stored.  If not the source image is compared to that of the
    figure.  This function also handles multiple images and finds the
    best matching image.  If `allow_resize` is True then the images
    are rescaled if they are not of the same size.
    """
    f_base, f_ext = os.path.splitext(os.path.abspath(img_fname))

    if not os.path.isfile(img_fname):
        # generate the image
        pngw = tvtk.PNGWriter(file_name=img_fname, input=src_img)
        pngw.write()
        if VERBOSE:
            print "Creating baseline image '%s'." % img_fname
        return

    pngr = tvtk.PNGReader(file_name=img_fname)
    pngr.update()

    if allow_resize:
        src_resample = tvtk.ImageResample(input=src_img,
                                          interpolate=1,
                                          interpolation_mode='cubic')
        img_resample = tvtk.ImageResample(input=pngr.output,
                                          interpolate=1,
                                          interpolation_mode='cubic')
        _set_scale(src_resample, img_resample)
        idiff = tvtk.ImageDifference(input=src_resample.output,
                                     image=img_resample.output)
    else:
        idiff = tvtk.ImageDifference(input=src_img, image=pngr.output)
    idiff.update()

    min_err = idiff.thresholded_error
    img_err = min_err
    best_img = img_fname

    err_index = 0
    count = 0
    if min_err > threshold:
        count = 1
        test_failed = 1
        err_index = -1
        while 1:  # keep trying images till we get the best match.
            new_fname = f_base + "_%d.png" % count
            if not os.path.exists(new_fname):
                # no other image exists.
                break
            # since file exists check if it matches.
            pngr.file_name = new_fname
            pngr.update()
            if allow_resize:
                _set_scale(src_resample, img_resample)
            idiff.update()
            alt_err = idiff.thresholded_error
            if alt_err < threshold:
                # matched,
                err_index = count
                test_failed = 0
                min_err = alt_err
                img_err = alt_err
                best_img = new_fname
                break
            else:
                if alt_err < min_err:
                    # image is a better match.
                    err_index = count
                    min_err = alt_err
                    img_err = alt_err
                    best_img = new_fname

            count = count + 1
        # closes while loop.

        if test_failed:
            _handle_failed_image(idiff, src_img, pngr, best_img)
            _print_image_error(img_err, err_index, f_base)
            msg = "Failed image test: %f\n" % idiff.thresholded_error
            raise AssertionError, msg
    # output the image error even if a test passed
    _print_image_success(img_err, err_index)
Exemplo n.º 12
0
 def __png_writer_default(self):
     return tvtk.PNGWriter(write_to_memory=True)
Exemplo n.º 13
0
m = tvtk.PolyDataMapper()

# Note that VTK's GetOutput method is special because it has two call
# signatures: GetOutput() and GetOutput(int N) (which gets the N'th
# output).  In tvtk it is represented as both a property and as a
# method.  Using the output property will work fine if all you want is
# the default output.  OTOH if you want the N'th output use
# get_output(N).
m.input = cs.output  # or m.input = cs.get_output()

# Create the actor and set its mapper.
a = tvtk.Actor(mapper=m)

# Create a Renderer, add the actor and set its background color.
ren = tvtk.Renderer(background=(0.1, 0.2, 0.4))
ren.add_actor(a)
ren.reset_camera()

# Create a RenderWindow, add the renderer and set its size.
rw = tvtk.RenderWindow(size=(300, 300))
rw.off_screen_rendering = 1
rw.add_renderer(ren)

w2if = tvtk.WindowToImageFilter()
w2if.magnification = 2
w2if.input = rw
ex = tvtk.PNGWriter()
ex.file_name = "example.png"
ex.input = w2if.output
ex.write()
Exemplo n.º 14
0
    def ipython_view(self, width, height, view={}):
        import ipywidgets as widgets
        from IPython.display import Image, display, clear_output

        renderer = tvtk.Renderer()
        for actor in self.scene.actor_list:
            renderer.add_actor(actor)
        renderer.background = (1, 1, 0.8)
        #
        renderer.reset_camera()
        camera = renderer.active_camera
        if "position" in view:
            camera.position = view['position']
        if "focal_point" in view:
            camera.focal_point = view['focal_point']
        if "view_up" in view:
            camera.view_up = view['view_up']
#
        renderWindow = tvtk.RenderWindow()
        renderWindow.off_screen_rendering = True
        renderWindow.add_renderer(renderer)
        renderWindow.size = (width, height)
        renderWindow.render()

        windowToImageFilter = tvtk.WindowToImageFilter()
        windowToImageFilter.input = renderWindow
        windowToImageFilter.update()
        #
        filename = "/dev/shm/temp_vtk_put.png"
        writer = tvtk.PNGWriter()
        writer.file_name = filename
        writer.write_to_memory = False
        writer.input_connection = windowToImageFilter.output_port
        writer.write()

        view_out = {}

        def show():
            clear_output(wait=True)
            renderer.reset_camera_clipping_range()
            renderer.modified()
            renderWindow.render()
            windowToImageFilter.input = renderWindow
            windowToImageFilter.modified()
            windowToImageFilter.update()
            writer.write()
            view_out.update({
                "position": tuple(camera.position),
                "view_up": tuple(camera.view_up),
                "focal_point": tuple(camera.focal_point)
            })
            return display(Image(filename), grp)

        def r_up(arg):
            camera.orthogonalize_view_up()
            camera.elevation(10)
            return show()

        def r_down(arg):
            camera.orthogonalize_view_up()
            camera.elevation(-10)
            return show()

        def r_left(arg):
            camera.orthogonalize_view_up()
            camera.azimuth(10)
            return show()

        def r_right(arg):
            camera.orthogonalize_view_up()
            camera.azimuth(-10)
            return show()

        def pan_left(arg):
            camera.orthogonalize_view_up()
            camera.yaw(-2)
            return show()

        def pan_right(arg):
            camera.orthogonalize_view_up()
            camera.yaw(2)
            return show()

        def pan_up(arg):
            camera.orthogonalize_view_up()
            camera.pitch(-2)
            return show()

        def pan_down(arg):
            camera.orthogonalize_view_up()
            camera.pitch(2)
            return show()

        def roll_left(arg):
            camera.roll(10)
            return show()

        def roll_right(arg):
            camera.roll(-10)
            return show()

        def zoom_in(arg):
            camera.dolly(1.2)
            return show()

        def zoom_out(arg):
            camera.dolly(0.8)
            return show()

        b1 = widgets.Button(description='\u2191')
        b1.on_click(r_up)
        b2 = widgets.Button(description='\u2193')
        b2.on_click(r_down)
        b3 = widgets.Button(description='\u2190')
        b3.on_click(r_left)
        b4 = widgets.Button(description='\u2192')
        b4.on_click(r_right)
        b5 = widgets.Button(description='\u21ba')
        b5.on_click(roll_left)
        b6 = widgets.Button(description='\u21bb')
        b6.on_click(roll_right)

        b7 = widgets.Button(description='+')
        b7.on_click(zoom_in)
        b8 = widgets.Button(description='-')
        b8.on_click(zoom_out)
        b9 = widgets.Button(description='\u2190')
        b9.on_click(pan_left)
        b10 = widgets.Button(description='\u2192')
        b10.on_click(pan_right)
        b11 = widgets.Button(description='\u2191')
        b11.on_click(pan_up)
        b12 = widgets.Button(description='\u2193')
        b12.on_click(pan_down)

        grp1 = widgets.HBox(border_style="solid",
                            border_width=1,
                            border_radius=5,
                            padding=2,
                            margin=1)
        grp1.children = [b1, b2, b3, b4, b5, b6]

        grp2 = widgets.HBox(border_style="solid",
                            border_width=1,
                            border_radius=5,
                            padding=2,
                            margin=1)
        grp2.children = [b7, b8, b9, b10, b11, b12]

        grp = widgets.HBox(children=[grp1, grp2])

        show()
        return view_out