Skip to content
This repository was archived by the owner on Dec 1, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 25 additions & 3 deletions aicsimageio/aics_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------------------
Expand Down Expand Up @@ -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"<AICSImage [{type(self.reader).__name__}]>"

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
19 changes: 19 additions & 0 deletions aicsimageio/tests/test_aics_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The """pythonic""" way to do this is assert not img.reader._bytes.closed 🙃

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I like this better tbh

img.close()

with AICSImage(resources_dir / filename) as img:
img.metadata
assert img.reader._bytes.closed is True

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one should definitely just be assert img.reader._bytes.closed