Skip to content

Commit 07dfe63

Browse files
committed
Use temp directory when converting labelings
Using a single name in the CWD has a risk of collision
1 parent 0788e10 commit 07dfe63

File tree

1 file changed

+18
-37
lines changed

1 file changed

+18
-37
lines changed

src/imagej/convert.py

Lines changed: 18 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import ctypes
66
import logging
77
import os
8+
import tempfile
89
from typing import Dict, List, Sequence, Union
910

1011
import imglyb
@@ -391,19 +392,15 @@ def labeling_to_imglabeling(ij: "jc.ImageJ", labeling: Labeling):
391392
"""
392393
labeling_io_service = ij.context().service(jc.LabelingIOService)
393394

394-
# Save the image on the Python side
395-
tmp_pth = "./tmp"
396-
_delete_labeling_files(tmp_pth)
397-
labeling.save_result(tmp_pth)
395+
# Create a unique temporary directory to avoid file collisions
396+
with tempfile.TemporaryDirectory(prefix="pyimagej_labeling_") as tmp_dir:
397+
# Save the image on the Python side
398+
tmp_pth = os.path.join(tmp_dir, "labeling")
399+
labeling.save_result(tmp_pth)
398400

399-
# Load the labeling on the Java side
400-
try:
401+
# Load the labeling on the Java side
401402
tmp_pth_json = tmp_pth + ".lbl.json"
402403
imglabeling = labeling_io_service.load(tmp_pth_json, JObject, JObject)
403-
except JException as exc:
404-
_delete_labeling_files(tmp_pth)
405-
raise exc
406-
_delete_labeling_files(tmp_pth)
407404

408405
return imglabeling
409406

@@ -418,22 +415,19 @@ def imglabeling_to_labeling(ij: "jc.ImageJ", imglabeling: "jc.ImgLabeling"):
418415
"""
419416
labeling_io_service = ij.context().service(jc.LabelingIOService)
420417

421-
# Save the image on the Python side
422-
tmp_pth = os.getcwd() + "/tmp"
423-
tmp_pth_json = tmp_pth + ".lbl.json"
424-
tmp_pth_tif = tmp_pth + ".tif"
425-
try:
426-
_delete_labeling_files(tmp_pth)
418+
# Create a unique temporary directory to avoid file collisions
419+
with tempfile.TemporaryDirectory(prefix="pyimagej_labeling_") as tmp_dir:
420+
# Save the labeling on the Java side
421+
tmp_pth = os.path.join(tmp_dir, "labeling")
422+
tmp_pth_json = tmp_pth + ".lbl.json"
423+
tmp_pth_tif = tmp_pth + ".tif"
424+
427425
imglabeling = ij.convert().convert(imglabeling, jc.ImgLabeling)
428-
labeling_io_service.save(
429-
imglabeling, tmp_pth_tif
430-
) # TODO: improve, likely utilizing the ImgLabeling's name
431-
except JException:
432-
print("Failed to save the data")
426+
labeling_io_service.save(imglabeling, tmp_pth_tif)
427+
428+
# Load the labeling on the Python side
429+
labeling = Labeling.from_file(tmp_pth_json)
433430

434-
# Load the labeling on the python side
435-
labeling = Labeling.from_file(tmp_pth_json)
436-
_delete_labeling_files(tmp_pth)
437431
return labeling
438432

439433

@@ -687,19 +681,6 @@ def _rename_xarray_dims(xarr, new_dims: Sequence[str]):
687681
return xarr.rename(dim_map)
688682

689683

690-
def _delete_labeling_files(filepath):
691-
"""
692-
Removes any Labeling data left over at filepath
693-
:param filepath: the filepath where Labeling (might have) saved data
694-
"""
695-
pth_json = filepath + ".lbl.json"
696-
pth_tif = filepath + ".tif"
697-
if os.path.exists(pth_tif):
698-
os.remove(pth_tif)
699-
if os.path.exists(pth_json):
700-
os.remove(pth_json)
701-
702-
703684
def _dim_order(hints: Dict):
704685
"""
705686
Extract the dim_order from the hints kwargs.

0 commit comments

Comments
 (0)