Skip to content

Commit

Permalink
Avoid using temp files when generating Pillow images (#682)
Browse files Browse the repository at this point in the history
* Avoid using temp file to generate Pillow images

* Avoid using temp files for generating dot images

* Apply code review suggestion

Co-authored-by: Matthew Treinish <[email protected]>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Oct 4, 2022
1 parent 8c28713 commit 51637af
Showing 1 changed file with 12 additions and 16 deletions.
28 changes: 12 additions & 16 deletions rustworkx/visualization/graphviz.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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],
Expand Down

0 comments on commit 51637af

Please sign in to comment.