diff --git a/README.md b/README.md index e5ae0fc70..ae9d2b644 100644 --- a/README.md +++ b/README.md @@ -31,10 +31,17 @@ from aicsimageio import AICSImage, imread im = imread("/path/to/your/file_or_buffer.ome.tiff") # For AICSImage object that -im = AICSImage("/path/to/your/file_or_buffer.ome.tiff") +with AICSImage("/path/to/your/file_or_buffer.ome.tiff") as im: + # use im object # To specify a known dimension order -im = AICSImage("/path/to/your/file_or_buffer.ome.tiff", known_dims="SYX") +with AICSImage("/path/to/your/file_or_buffer.ome.tiff", known_dims="SYX") as im: + # use im object + +# if you instantiate an AICSImage: +im = AICSImage("/path/to/your/file_or_buffer.ome.tiff") +# you should close it when done: +im.close() # Image data is stored in `data` attribute im.data # returns the image data numpy array diff --git a/aicsimageio/aics_image.py b/aicsimageio/aics_image.py index b41264259..8d5899b8a 100644 --- a/aicsimageio/aics_image.py +++ b/aicsimageio/aics_image.py @@ -33,18 +33,29 @@ class AICSImage: zstack_t10 = data[0, 10, 0, :, :, :] # access the S=0, T=10, C=0 "ZYX" cube - File Examples ------------- + + with AICSImage("filename.ome.tif") as img: + # do work with img + data = img.data + metadata = img.metadata + # img will be closed automatically at end of scope + OmeTif img = AICSImage("filename.ome.tif") + img.close() CZI (Zeiss) img = AICSImage("filename.czi") or AICSImage("filename.czi", max_workers=8) + img.close() Tiff img = AICSImage("filename.tif") + img.close() Png/Gif/... img = AICSImage("filename.png") + img.close() img = AICSImage("filename.gif") + img.close() Bytestream Examples ------------------- @@ -291,9 +302,20 @@ def get_channel_names(self, scene: int = 0): names = [str(i) for i in range(self.size_c)] return names + def close(self): + self.reader.close() + def __repr__(self) -> str: return f"" + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_val, exc_tb): + self.close() + -def imread(data: types.ImageLike, **kwargs): - return AICSImage(data, **kwargs).data +def imread(data: types.ImageLike, **kwargs) -> np.ndarray: + with AICSImage(data, **kwargs) as image: + imagedata = image.data + return imagedata diff --git a/aicsimageio/tests/test_aics_image.py b/aicsimageio/tests/test_aics_image.py index 83148174d..65a12f497 100644 --- a/aicsimageio/tests/test_aics_image.py +++ b/aicsimageio/tests/test_aics_image.py @@ -197,3 +197,22 @@ def test_imread(resources_dir, filename, expected_shape): def test_channel_names(resources_dir, filename, expected_channel_names): img = AICSImage(resources_dir / filename) assert img.get_channel_names() == expected_channel_names + + +@pytest.mark.parametrize( + "filename", + [ + PNG_FILE, + TIF_FILE, + CZI_FILE, + OME_FILE, + ], +) +def test_aicsimage_close(resources_dir, filename): + img = AICSImage(resources_dir / filename) + assert img.reader._bytes.closed is False + img.close() + + with AICSImage(resources_dir / filename) as img: + img.metadata + assert img.reader._bytes.closed is True