-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ImageInterface
#1190
Open
h-mayorquin
wants to merge
13
commits into
main
Choose a base branch
from
add_imaing_interface
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add ImageInterface
#1190
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
41774f7
add tests and draft of interface
h-mayorquin 13f5f9c
fix imports
h-mayorquin 995a0c2
Merge branch 'main' into add_imaing_interface
h-mayorquin ee14034
changelog
h-mayorquin af01d94
changelog
h-mayorquin 8399959
add support for LA to RGBA
h-mayorquin f89da95
merge
h-mayorquin a19e661
Merge branch 'main' into add_imaing_interface
h-mayorquin 17e1ee5
docs
h-mayorquin 4941763
remove images
h-mayorquin fac52aa
Merge branch 'main' into add_imaing_interface
h-mayorquin 78f810e
small improvements
h-mayorquin d087e15
move doc to conversion gallery
h-mayorquin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
Image Interface | ||
=============== | ||
|
||
The ImageInterface allows you to convert various image formats (PNG, JPG, TIFF) to NWB. It supports different color modes and efficiently handles image loading to minimize memory usage. | ||
|
||
Supported Image Modes | ||
--------------------- | ||
|
||
The interface supports the following PIL image modes: | ||
|
||
- L (grayscale) → GrayscaleImage | ||
- RGB → RGBImage | ||
- RGBA → RGBAImage | ||
- LA (luminance + alpha) → RGBAImage (automatically converted) | ||
|
||
Example Usage | ||
------------- | ||
|
||
Here's an example demonstrating how to use the ImageInterface with different image modes: | ||
|
||
.. code-block:: python | ||
|
||
from datetime import datetime | ||
from pathlib import Path | ||
from neuroconv.datainterfaces import ImageInterface | ||
from pynwb import NWBHDF5IO, NWBFile | ||
|
||
# Create example images of different modes | ||
from PIL import Image | ||
import numpy as np | ||
|
||
# Create a temporary directory for our example images | ||
from tempfile import mkdtemp | ||
image_dir = Path(mkdtemp()) | ||
|
||
# Create example images | ||
# RGB image (3 channels) | ||
rgb_array = np.random.randint(0, 255, (100, 100, 3), dtype=np.uint8) | ||
rgb_image = Image.fromarray(rgb_array, mode='RGB') | ||
rgb_image.save(image_dir / 'rgb_image.png') | ||
|
||
# Grayscale image (L mode) | ||
gray_array = np.random.randint(0, 255, (100, 100), dtype=np.uint8) | ||
gray_image = Image.fromarray(gray_array, mode='L') | ||
gray_image.save(image_dir / 'gray_image.png') | ||
|
||
# RGBA image (4 channels) | ||
rgba_array = np.random.randint(0, 255, (100, 100, 4), dtype=np.uint8) | ||
rgba_image = Image.fromarray(rgba_array, mode='RGBA') | ||
rgba_image.save(image_dir / 'rgba_image.png') | ||
|
||
# LA image (luminance + alpha) | ||
la_array = np.random.randint(0, 255, (100, 100, 2), dtype=np.uint8) | ||
la_image = Image.fromarray(la_array, mode='LA') | ||
la_image.save(image_dir / 'la_image.png') | ||
|
||
# Initialize the image interface | ||
interface = ImageInterface(folder_path=str(image_dir)) | ||
|
||
# Create a basic NWBFile | ||
nwbfile = NWBFile( | ||
session_description="Image interface example session", | ||
identifier="IMAGE123", | ||
session_start_time=datetime.now().astimezone(), | ||
experimenter="Dr. John Doe", | ||
lab="Image Processing Lab", | ||
institution="Neural Image Institute", | ||
experiment_description="Example experiment demonstrating image conversion", | ||
) | ||
|
||
# Add the images to the NWB file | ||
interface.add_to_nwbfile(nwbfile) | ||
|
||
# Write the NWB file | ||
nwb_path = Path("image_example.nwb") | ||
with NWBHDF5IO(nwb_path, "w") as io: | ||
io.write(nwbfile) | ||
|
||
# Read the NWB file to verify | ||
with NWBHDF5IO(nwb_path, "r") as io: | ||
nwbfile = io.read() | ||
# Access the images container | ||
images_container = nwbfile.acquisition["images"] | ||
print(f"Number of images: {len(images_container.images)}") | ||
# Print information about each image | ||
for name, image in images_container.images.items(): | ||
print(f"\nImage name: {name}") | ||
print(f"Image type: {type(image).__name__}") | ||
print(f"Image shape: {image.data.shape}") | ||
|
||
Key Features | ||
------------ | ||
|
||
1. **Memory Efficiency**: Uses an iterator pattern to load images only when needed, making it suitable for large images or multiple images. | ||
|
||
2. **Automatic Mode Conversion**: Handles LA (luminance + alpha) to RGBA conversion automatically while maintaining image information. | ||
|
||
3. **Input Methods**: | ||
- List of files: ``interface = ImageInterface(file_paths=["image1.png", "image2.jpg"])`` | ||
- Directory: ``interface = ImageInterface(folder_path="images_directory")`` | ||
|
||
4. **Flexible Storage Location**: Images can be stored in either acquisition or stimulus: | ||
.. code-block:: python | ||
|
||
# Store in acquisition (default) | ||
interface = ImageInterface(file_paths=["image.png"], images_location="acquisition") | ||
|
||
# Store in stimulus | ||
interface = ImageInterface(file_paths=["image.png"], images_location="stimulus") | ||
|
||
Installation | ||
------------ | ||
|
||
To use the ImageInterface, install neuroconv with the image extra: | ||
|
||
.. code-block:: bash | ||
|
||
pip install "neuroconv[image]" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
"""Image data interfaces.""" | ||
|
||
from .imageinterface import ImageInterface | ||
|
||
__all__ = ["ImageInterface"] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ben mentioned that a better place for this might be the conversion gallery and I think he is right.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I moved it.