Skip to content

Commit

Permalink
added rawpy plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
tahseenadit committed Feb 20, 2024
1 parent 085ce27 commit ac77a7c
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 3 deletions.
4 changes: 2 additions & 2 deletions imageio/config/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ def reset(self):
),
FileExtension(
extension=".nef",
priority=["RAW-FI"],
priority=["rawpy", "RAW-FI"],
),
FileExtension(
extension=".nhdr",
Expand Down Expand Up @@ -762,7 +762,7 @@ def reset(self):
),
FileExtension(
extension=".raw",
priority=["RAW-FI", "LYTRO-ILLUM-RAW", "LYTRO-F01-RAW"],
priority=["rawpy", "RAW-FI", "LYTRO-ILLUM-RAW", "LYTRO-F01-RAW"],
),
FileExtension(
extension=".rdc",
Expand Down
4 changes: 3 additions & 1 deletion imageio/config/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ def partial_legacy_plugin(request):
known_plugins["SPE"] = PluginConfig(
name="spe", class_name="SpePlugin", module_name="imageio.plugins.spe"
)

known_plugins["rawpy"] = PluginConfig(
name="rawpy", class_name="RawPyPlugin", module_name="imageio.plugins.rawpy"
)

# Legacy plugins
# ==============
Expand Down
66 changes: 66 additions & 0 deletions imageio/plugins/rawpy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# -*- coding: utf-8 -*-
# imageio is distributed under the terms of the (new) BSD License.

""" Read/Write images using rawpy.
rawpy is an easy-to-use Python wrapper for the LibRaw library.
It also contains some extra functionality for finding and repairing hot/dead pixels.
"""

import rawpy
import numpy as np

from ..core.request import URI_BYTES, InitializationError, IOMode, Request
from ..core.v3_plugin_api import PluginV3


class RawPyPlugin(PluginV3):
"""A class representing the rawpy plugin.
This class is a subclass of PluginV3.
Methods
-------
.. autosummary::
:toctree: _plugins/rawpy
RawPyPlugin.read
"""
def __init__(self, request: Request) -> None:
"""Instantiates a new rawpy plugin object
parameters
----------
request: {Request}
A request object representing the resource to be operated on.
"""

super().__init__(request)

self._image: rawpy = None
self._image_file = request.get_file()

def read(self, *, index: int = 0) -> np.ndarray:
"""Read Raw Image.
Returns
-------
image: ndimage
The image data
"""

if self._request.mode.io_mode == IOMode.read:
try:
with rawpy.imread(self._image_file) as raw_image:
self._image = raw_image.raw_image
except (AttributeError, TypeError, rawpy.LibRawIOError) as ex:
print(ex)
except (rawpy.NotSupportedError, rawpy.LibRawError, rawpy.LibRawFatalError, rawpy.LibRawNonFatalError) as ex:
print(ex)
except Exception as ex:
print(ex)
else:
print("Read is not supported!")

return np.asarray(self._image)

1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
"tifffile": ["tifffile"],
"pyav": ["av"],
"pillow-heif": ["pillow-heif"],
"rawpy": ["rawpy"]
}

cpython_only_plugins = {
Expand Down
20 changes: 20 additions & 0 deletions tests/test_rawpy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""Tests for rawpy plugin
"""

import os
import rawpy
import imageio.v3 as iio


def test_nef_local(test_images):
"""Test for reading .nef file from .test_images dir.
"""
# Check image path
im_path = str(test_images / "Nikon.nef")
assert os.path.exists(im_path)

# Test if plugin's content mathces rawpy content
im_plugin = iio.imread(im_path)
im_rawpy = rawpy.imread(im_path)
assert im_plugin.all() == im_rawpy.raw_image.all()

0 comments on commit ac77a7c

Please sign in to comment.