diff --git a/rustworkx/visualization/graphviz.py b/rustworkx/visualization/graphviz.py index 96e84cc6d..4d5198814 100644 --- a/rustworkx/visualization/graphviz.py +++ b/rustworkx/visualization/graphviz.py @@ -6,10 +6,9 @@ # copyright notice, and modified files need to carry a notice indicating # that they have been altered from the originals. -import os import subprocess import tempfile -import uuid +import io try: from PIL import Image @@ -185,20 +184,17 @@ def node_attr(node): prog = method if not filename: - with tempfile.TemporaryDirectory() as tmpdirname: - filename = f"graphviz_draw_{str(uuid.uuid4())}.{output_format}" - tmp_path = os.path.join(tmpdirname, filename) - subprocess.run( - [prog, "-T", output_format, "-o", tmp_path], - input=dot_str, - check=True, - encoding="utf8", - text=True, - ) - with Image.open(tmp_path) as temp_image: - image = temp_image.copy() - os.remove(tmp_path) - return image + dot_result = subprocess.run( + [prog, "-T", output_format], + input=dot_str.encode("utf-8"), + capture_output=True, + encoding=None, + check=True, + text=False, + ) + dot_bytes_image = io.BytesIO(dot_result.stdout) + image = Image.open(dot_bytes_image) + return image else: subprocess.run( [prog, "-T", output_format, "-o", filename],