def __call__(self, expr): r'''A replacement for Ajbad's show function for IPython Notebook. ''' from abjad.tools import systemtools from abjad.tools import topleveltools from IPython.core.display import display_png assert systemtools.IOManager.find_executable('lilypond') assert systemtools.IOManager.find_executable('convert') assert hasattr(expr, '__illustrate__') temporary_directory = tempfile.mkdtemp() temporary_file_path = os.path.join( temporary_directory, 'output.png', ) result = topleveltools.persist(expr).as_png(temporary_file_path) pngs = [] for file_path in result[0]: command = 'convert {file_path} -trim {file_path}' command = command.format(file_path=file_path) systemtools.IOManager.spawn_subprocess(command) with open(file_path, 'rb') as file_pointer: file_contents = file_pointer.read() pngs.append(file_contents) shutil.rmtree(temporary_directory) for png in pngs: display_png(png, raw=True)
def _dot(self, args, string): """ Run the Graphviz dot command. """ # Parse the arguments image = run_dot(string, args.options, format=args.format) if args.format == 'png': display_png(image, raw=True) elif args.format == 'svg': display_svg(image, raw=True)
def display_matrix_image(mat): img = Image.new('L',size=(mat.dim()[1],mat.dim()[0])) for i in range(mat.dim()[0]): for j in range(mat.dim()[1]): img.putpixel((j,i),mat[i,j]) b=BytesIO() img.save(b,format='png') data = b.getvalue() display.display_png(data, raw=True)
def dot(self, line, cell): """Draw a figure using Graphviz dot command.""" args = parse_argstring(self.dot, line) image = run_dot(cell, args.options, format=args.format) if args.format == 'png': display_png(image, raw=True) elif args.format == 'svg': display_svg(image, raw=True)
def hierarchy(self, parameter_s=''): """Draw hierarchy of a given class.""" args = parse_argstring(self.hierarchy, parameter_s) objects = map(self.shell.ev, args.object) clslist = map(self._object_to_class, objects) namelist = map(self._class_name, clslist) ig = FoldedInheritanceGraph( namelist, '', width=args.name_width) code = ig.generate_dot('inheritance_graph', graph_attrs={'rankdir': args.rankdir, 'size': '"{0}"'.format(args.size)}) stdout = run_dot(code, format='png') display_png(stdout, raw=True)
def dot(self, line="", cell=None): """Draw a figure using Graphviz dot command.""" args = parse_argstring(self.dot, line) if cell is None: sh = get_ipython() cell = sh.ev(args.code[0]) image = run_dot(cell, [], format=args.format) if args.format == 'png': display_png(image, raw=True) elif args.format == 'svg': display_svg(image, raw=True)
def hierarchy(self, parameter_s=''): """Draw hierarchy of a given class.""" args = parse_argstring(self.hierarchy, parameter_s) objects = map(self.shell.ev, args.object) clslist = map(self._object_to_class, objects) namelist = map(self._class_name, clslist) ig = FoldedInheritanceGraph(namelist, '', width=args.name_width) code = ig.generate_dot('inheritance_graph', graph_attrs={ 'rankdir': args.rankdir, 'size': '"{0}"'.format(args.size) }) stdout = run_dot(code, format='png') display_png(stdout, raw=True)
def show_image(arr): resized = imutils.resize(arr, width=800) png_to_show = cv2.imencode('.png', resized)[1].tostring() display_png(png_to_show, raw=True) png_to_save = cv2.imencode('.png', arr)[1].tostring() try: os.makedirs(temp_images_dir) except OSError as exception: if exception.errno != errno.EEXIST: raise file_name = datetime.datetime.now().isoformat().replace(':', '_') + '.png' file_path = os.path.join(temp_images_dir, file_name) with open(file_path, 'w') as f: f.write(png_to_save) url = 'http://localhost:8888/files/%s' % file_path display_html('<a href="%s" target="_blank">%s</a>' % (url, file_name), raw=True)
def display(graph): ''' Visualises the input graph. ''' display_png(drawDot(graph), raw=True)
def display_pil_image(im): """Displayhook function for PIL Images, rendered as PNG.""" b=BytesIO() img.save(b,format='png') data = b.getvalue() display.display_png(data, raw=True)
def show(expr): """A replacement for Ajbad's show function for IPython Notebook""" assert '__illustrate__' in dir(expr) pngs = _get_png(expr) for png in pngs: display_png(png, raw=True)
def __call__( self, expr, layout='dot', graph_attributes=None, node_attributes=None, edge_attributes=None, **kwargs ): r'''A replacement for Ajbad's graph function for IPython Notebook. ''' from abjad.tools import systemtools from IPython.core.display import display_png if isinstance(expr, str): graphviz_format = expr else: assert hasattr(expr, '__graph__') graphviz_graph = expr.__graph__(**kwargs) if graph_attributes: graphviz_graph.attributes.update(graph_attributes) if node_attributes: graphviz_graph.node_attributes.update(node_attributes) if edge_attributes: graphviz_graph.edge_attributes.update(edge_attributes) graphviz_format = str(graphviz_graph) valid_layouts = ( 'circo', 'dot', 'fdp', 'neato', 'osage', 'sfdp', 'twopi', ) assert layout in valid_layouts message = 'cannot find `{}` command-line tool.' message = message.format(layout) message += ' Please download Graphviz from graphviz.org.' assert systemtools.IOManager.find_executable(layout), message assert systemtools.IOManager.find_executable('convert') temporary_directory = tempfile.mkdtemp() dot_path = os.path.join(temporary_directory, 'graph.dot') pdf_path = os.path.join(temporary_directory, 'graph.pdf') png_path = os.path.join(temporary_directory, 'graph.png') with open(dot_path, 'w') as file_pointer: file_pointer.write(graphviz_format) command = '{} -v -Tpdf {} -o {}' command = command.format(layout, dot_path, pdf_path) subprocess.call(command, shell=True) command = 'convert {} -trim {}' command = command.format(pdf_path, png_path) subprocess.call(command, shell=True) systemtools.IOManager.spawn_subprocess(command) with open(png_path, 'rb') as file_pointer: png = file_pointer.read() shutil.rmtree(temporary_directory) display_png(png, raw=True)