Browse Source

Merge pull request #97 from jimrybarski/96-context-manager

resolves #96: added context manager
feature/load_slices
Jim Rybarski 9 years ago
parent
commit
07cae9da25
2 changed files with 16 additions and 1 deletions
  1. +8
    -0
      README.md
  2. +8
    -1
      nd2reader/interface.py

+ 8
- 0
README.md View File

@ -52,6 +52,14 @@ You can also get some metadata about the nd2 programatically:
30528 30528
``` ```
`Nd2` is also a context manager, if you care about that sort of thing:
```
>>> import nd2reader
>>> with nd2reader.Nd2("/path/to/my_images.nd2") as nd2:
... for image in nd2:
... do_something(image)
### Images ### Images
`Image` objects are just Numpy arrays with some extra metadata bolted on: `Image` objects are just Numpy arrays with some extra metadata bolted on:


+ 8
- 1
nd2reader/interface.py View File

@ -12,12 +12,19 @@ class Nd2(object):
""" """
def __init__(self, filename): def __init__(self, filename):
self._filename = filename
self._fh = open(filename, "rb") self._fh = open(filename, "rb")
major_version, minor_version = get_version(self._fh) major_version, minor_version = get_version(self._fh)
parser = get_parser(self._fh, major_version, minor_version) parser = get_parser(self._fh, major_version, minor_version)
self._driver = parser.driver self._driver = parser.driver
self._metadata = parser.metadata self._metadata = parser.metadata
self._filename = filename
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
if self._fh is not None:
self._fh.close()
def __repr__(self): def __repr__(self):
return "\n".join(["<ND2 %s>" % self._filename, return "\n".join(["<ND2 %s>" % self._filename,


Loading…
Cancel
Save