Converting dot to png in python

Question:

I have a dot file generated from my code and want to render it in my output. For this i have seen on the net that the command is something like this on cmd

dot -Tpng InputFile.dot -o OutputFile.png  for Graphviz

But my problem is that I want to use this inbuilt in my python program.

How can i do so ??

I looked at pydot but can’t seem to find an answer in there…..

Asked By: user506710

||

Answers:

pydot needs the GraphViz binaries to be installed anyway, so if you’ve already generated your dot file you might as well just invoke dot directly yourself. For example:

from subprocess import check_call
check_call(['dot','-Tpng','InputFile.dot','-o','OutputFile.png'])
Answered By: Mark Longair

You can use pygraphviz. Once you have a graph loaded, you can do

graph.draw('file.png')
Answered By: nmichaels

Load the file with pydot.graph_from_dot_file to get a pydot.Dot class instance. Then write it to a PNG file with the write_png method.

import pydot

(graph,) = pydot.graph_from_dot_file('somefile.dot')
graph.write_png('somefile.png')
Answered By: Judge Maygarden

You can try:

import os
os.environ["PATH"] += os.pathsep + 'C:/Program Files (x86)/Graphviz2.38/bin/'
os.system('dot -Tpng random.dot -o random.png')
Answered By: Mauricio Carrero

You can use graphviz:

# Convert a .dot file to .png
from graphviz import render
render('dot', 'png', 'fname.dot')

# To render an existing file in a notebook
from graphviz import Source
Source.from_file("fname.dot")
Answered By: Sam Perry

1st Solution)

Going further with the approach of @Mauricio Carrero by setting the PATH inside the script (the same PATH set in the environment variables does not have this effect!):

import os
import pydotplus
from sklearn.tree import export_graphviz

os.environ['PATH'] = os.environ['PATH']+';' + r'C:UsersAdminAnaconda3Librarybingraphviz'

# first export the dot file only if needed
export_graphviz(clf, out_file=filename + ".dot", feature_names = feature_names)
# now generate the dot_data again
dot_data = export_graphviz(clf, out_file=None, feature_names = feature_names)
graph = pydotplus.graphviz.graph_from_dot_data(dot_data)
graph.write_png(filename + "_gv.png")

This made it possible to save the dot_data to png. Choose your own local paths, you might also have installed graphviz in `C:/Program Files (x86)/Graphviz2.38/bin/

This solution also came from Sarunas answer here:
https://datascience.stackexchange.com/questions/37428/graphviz-not-working-when-imported-inside-pydotplus-graphvizs-executables-not

2nd Solution)

You can also avoid the error

Exception has occurred: InvocationException
GraphViz's executables not found

by simply giving it what it wants, as it asks for the executables of the graphviz object:

graph = pydotplus.graphviz.graph_from_dot_data(dot_data)
# graph is now a new Dot object!
# That is why we need to set_graphviz_executables for every new instance
# This cannot be set globally but must be set again and again
# that is why the PATH solution (1st Solution) above seems much better to me
# see the docs in https://pydotplus.readthedocs.io/reference.html
pathCur = 'C:\Program Files (x86)\Graphviz2.38\bin\'
graph.set_graphviz_executables({'dot': pathCur+'dot.exe', 'twopi': pathCur +'twopi.exe', 'neato': pathCur+'neato.exe', 'circo': pathCur+'circo.exe', 'fdp': pathCur+'fdp.exe'})
graph.write_png(filename + "_gv.png")

p.s:
These 2 approaches were the only solutions working for me after 2 hours of calibrating erroneuos installations and full uninstall and install again, all varieties of PATH variables, external and internal graphviz installation, python-graphviz, pygraphviz and all of the solutions I could find in here, or in
Convert decision tree directly to png
or in
https://datascience.stackexchange.com/questions/37428/graphviz-not-working-when-imported-inside-pydotplus-graphvizs-executables-not?newreg=a789aadc5d4b4975949afadd3919fe55

For conda python-graphviz, I got constant installation errors like

InvalidArchiveError('Error with archive C:\Users\Admin\Anaconda3\pkgs\openssl-1.1.1d-he774522_20ffr2kor\pkg-openssl-1.1.1d-he774522_2.tar.zst.  You probably need to delete and re-download or re-create this file.  Message from libarchive was:nnCould not unlink')

For conda install graphviz, I got

InvalidArchiveError('Error with archive C:\Users\Admin\Anaconda3\pkgs\openssl-1.1.1d-he774522_21ww0bpcs\pkg-openssl-1.1.1d-he774522_2.tar.zst.  You probably need to delete and re-download or re-create this file.  Message from libarchive was:nnCould not unlink')

pygraphviz needs MS Visual C++ which I did not want to install:

error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": https://visualstudio.microsoft.com/downloads/

In the end, no guide was working to really set the PATH variables correctly except for the 1st Solution approach.

Here is the code that worked for me on windows 11 system using a pycharm terminal using venv python 3.8 interpreter.

from graphviz import render
import os
os.environ['PATH'] = os.environ['PATH']+';' + r"C:Program FilesGraphvizbin" #find binaries of graphviz and add to path
render('dot','png','classes.dot')

this will create a classes.dot.pg file (I don’t know how to fix the name but this is a png file you can open)

The classes dot was generated on terminal using

pyreverse package_path

pyreverse comes with pylint.

Installations:

pip install pylint

(install pylint only if you are creating classes.dot file)

pip install graphviz

Answered By: Sagar Dollin
from graphviz import render
dot.render(directory='doctest-output', view=True)
Answered By: Dave

This would create a graph ‘a’ to ‘b’ and save it as a png file.

code:

from graphviz import Digraph

dot = Digraph()
dot.node('a')
dot.node('b')
dot.edge('a','b')

dot. Render("sample.png")
Answered By: pyComali
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.