示例#1
0
文件: ipython.py 项目: baoboa/chemlab
def showmol(mol, style="ball-and-stick", width=300, height=300):
    v = QtViewer()
    w = v.widget

    if style == "ball-and-stick":
        bs = v.add_renderer(BallAndStickRenderer, mol.r_array, mol.type_array, mol.bonds)
    elif style == "vdw":
        sr = v.add_renderer(AtomRenderer, mol.r_array, mol.type_array, backend="impostors")

    w.camera.autozoom(mol.r_array)

    # Make sure to finish everything
    glBindTexture(w.textures["color"])
    glActiveTexture(GL_TEXTURE0)
    data = glGetTexImage(GL_TEXTURE_2D, 0, RGB, GL_UNSIGNED_BYTE)
    # data = glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE)    # Make pil image to save as png
    image = pil_Image.fromstring("RGB", (width, height), data)

    b = BytesIO()
    image.save(b, format="png")
    data = b.getvalue()

    # Cleanup
    del v
    del w

    # Save as png
    return ipy_Image(data=data)
示例#2
0
def showmol(mol, style='ball-and-stick',
            width=300, height=300):
    v = QtViewer()
    w = v.widget
    
    if style == 'ball-and-stick':
        bs = v.add_renderer(BallAndStickRenderer,
                            mol.r_array,
                            mol.type_array,
                            mol.bonds)
    elif style == 'vdw':
        sr = v.add_renderer(AtomRenderer, mol.r_array, mol.type_array,
                            backend='impostors')

    w.camera.autozoom(mol.r_array)
    
    # Make sure to finish everything
    image = w.toimage(width, height)
    b = BytesIO()
    image.save(b, format='png')
    data = b.getvalue()

    # Cleanup
    del v
    del w
    
    # Save as png
    return ipy_Image(data=data)
示例#3
0
def showsys(sys, width=400, height=400):
    v = QtViewer()
    w = v.widget

    sr = v.add_renderer(AtomRenderer, sys.r_array, sys.type_array,
                        backend='impostors')
    
    if sys.box_vectors is not None:
        v.add_renderer(BoxRenderer, sys.box_vectors)
    
    w.camera.autozoom(sys.r_array)
    
    w.camera.orbit_y(3.14/4)    
    w.camera.orbit_x(3.14/4)

    image = w.toimage(width, height)
    b = BytesIO()
    image.save(b, format='png')
    data = b.getvalue()
    
    # Cleanup
    del v
    del w
    # Save as png
    return ipy_Image(data=data)
示例#4
0
def showsys(sys, width=400, height=400):
    v = QtViewer()
    w = v.widget
    w.initializeGL()

    
    w.resize(width, height)
    w.resizeGL(width, height)

    sr = v.add_renderer(AtomRenderer, sys.r_array, sys.type_array,
                        backend='impostors')
    
    if sys.box_vectors is not None:
        v.add_renderer(BoxRenderer, sys.box_vectors)
    
    w.camera.autozoom(sys.r_array)
    
    w.camera.orbit_y(3.14/4)    
    w.camera.orbit_x(3.14/4)

    w.paintGL()
    # Make sure to finish everything
    data = glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE)

    # Make pil image to save as png
    image = pil_Image.fromstring('RGB', (width, height), data)
    b = BytesIO()
    image.save(b, format='png')
    data = b.getvalue()
    
    # Cleanup
    del v
    del w
    # Save as png
    return ipy_Image(data=data)
示例#5
0
def showsys(sys, width=400, height=400):
    v = QtViewer()
    w = v.widget

    sr = v.add_renderer(AtomRenderer,
                        sys.r_array,
                        sys.type_array,
                        backend='impostors')

    if sys.box_vectors is not None:
        v.add_renderer(BoxRenderer, sys.box_vectors)

    w.camera.autozoom(sys.r_array)

    w.camera.orbit_y(3.14 / 4)
    w.camera.orbit_x(3.14 / 4)

    image = w.toimage(width, height)
    b = BytesIO()
    image.save(b, format='png')
    data = b.getvalue()

    # Cleanup
    del v
    del w
    # Save as png
    return ipy_Image(data=data)
示例#6
0
def showmol(mol, style='ball-and-stick', width=300, height=300):
    v = QtViewer()
    w = v.widget

    if style == 'ball-and-stick':
        bs = v.add_renderer(BallAndStickRenderer, mol.r_array, mol.type_array,
                            mol.bonds)
    elif style == 'vdw':
        sr = v.add_renderer(AtomRenderer,
                            mol.r_array,
                            mol.type_array,
                            backend='impostors')

    w.camera.autozoom(mol.r_array)

    # Make sure to finish everything
    image = w.toimage(width, height)
    b = BytesIO()
    image.save(b, format='png')
    data = b.getvalue()

    # Cleanup
    del v
    del w

    # Save as png
    return ipy_Image(data=data)
示例#7
0
    def visualise(self, images, columns=1, width=None, height=None): 
        """ visualise image(s) in IPython
        
        When this object is returned by an input cell or passed to the
        display function, it will result in the image being displayed
        in the frontend.
        
        Parameters
        ----------
        images : list/single PIL.Image or (x,y)
            (x,y) denotes a blank space of size x,y e.g. [img1,(100,0),img2]
        columns : int
            number of image columns 
        width : int
            Width to which to constrain the image in html
        height : int
            Height to which to constrain the image in html
        
        Returns
        -------
        image : IPython.display.Image

        """        
        try:
            img_iter = iter(images)
        except TypeError:
            img_iter = iter([images])
        
        img_columns = []
        img_rows = []
        for image in img_iter: 
            
            # add blank
            try:
                x,y = image
                image = Image.new("RGB", (x,y), "white")
            except TypeError:
                pass
                
            if len(img_columns) < columns:
                img_columns.append(image)
            else:
                img_rows.append(self._concat_images_horizontal(img_columns))
                img_columns = [image]
        if img_columns:
            img_rows.append(self._concat_images_horizontal(img_columns))
        image = self._concat_images_vertical(img_rows)
        
        b = BytesIO()
        image.save(b, format='png')
        data = b.getvalue()
        b.close() #del b
        
        return ipy_Image(data=data, width=width, height=height)
示例#8
0
    def visualise(self, images, columns=1, width=None, height=None):
        """ visualise image(s) in IPython
        
        When this object is returned by an input cell or passed to the
        display function, it will result in the image being displayed
        in the frontend.
        
        Parameters
        ----------
        images : list/single PIL.Image or (x,y)
            (x,y) denotes a blank space of size x,y e.g. [img1,(100,0),img2]
        columns : int
            number of image columns 
        width : int
            Width to which to constrain the image in html
        height : int
            Height to which to constrain the image in html
        
        Returns
        -------
        image : IPython.display.Image

        """
        try:
            img_iter = iter(images)
        except TypeError:
            img_iter = iter([images])

        img_columns = []
        img_rows = []
        for image in img_iter:

            # add blank
            try:
                x, y = image
                image = Image.new("RGB", (x, y), "white")
            except TypeError:
                pass

            if len(img_columns) < columns:
                img_columns.append(image)
            else:
                img_rows.append(self._concat_images_horizontal(img_columns))
                img_columns = [image]
        if img_columns:
            img_rows.append(self._concat_images_horizontal(img_columns))
        image = self._concat_images_vertical(img_rows)

        b = BytesIO()
        image.save(b, format='png')
        data = b.getvalue()
        b.close()  #del b

        return ipy_Image(data=data, width=width, height=height)
示例#9
0
    def _show_molecule(self,
                       molecule,
                       active=False,
                       ball_stick=False,
                       zoom=1.,
                       width=300,
                       height=300,
                       rotations=[[0., 0., 0.]],
                       colorlist=[],
                       lines=[],
                       axis_length=0,
                       linestyle='impostors'):

        if active:
            return self._view_molecule(molecule,
                                       ball_stick=ball_stick,
                                       colorlist=colorlist)
        else:
            drawlines = lines[:]
            if axis_length:
                drawlines.append([(-1 * axis_length, 0, 0),
                                  (axis_length, 0, 0), 'red', 'dark_red', 3,
                                  True])
                drawlines.append([(0, -1 * axis_length, 0),
                                  (0, axis_length, 0), 'light_green',
                                  'dark_green', 3, True])
                drawlines.append([(0, 0, -1 * axis_length),
                                  (0, 0, axis_length), 'light_blue',
                                  'dark_blue', 3, True])

            images = []
            for rotation in rotations:
                images.append(
                    self._image_molecule(molecule,
                                         ball_stick=ball_stick,
                                         colorlist=colorlist,
                                         rotation=rotation,
                                         zoom=zoom,
                                         width=width,
                                         height=width,
                                         lines=drawlines,
                                         linestyle=linestyle))
            image = self._concat_images_horizontal(images)
            del images

            b = BytesIO()
            image.save(b, format='png')
            return ipy_Image(data=b.getvalue())
示例#10
0
def showmol(mol, style='ball-and-stick',
            width=300, height=300):
    v = QtViewer()
    w = v.widget
    w.initializeGL()
    
    w.resize(width, height)
    w.resizeGL(width, height)
    
    if style == 'ball-and-stick':
        bs = v.add_renderer(BallAndStickRenderer,
                            mol.r_array,
                            mol.type_array,
                            mol.bonds)
    elif style == 'vdw':
        sr = v.add_renderer(AtomRenderer, mol.r_array, mol.type_array,
                            backend='impostors')

    w.camera.autozoom(mol.r_array)
    
    w.paintGL()
    # Make sure to finish everything
    
    data = glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE)
    # Make pil image to save as png
    image = pil_Image.fromstring('RGB', (width, height), data)
    b = BytesIO()
    image.save(b, format='png')
    data = b.getvalue()
    
    # Cleanup
    del v
    del w
    
    # Save as png
    return ipy_Image(data=data)