Skip to content

Commit

Permalink
raw format parsing added for arw, dng, cr2 yurijmikhalevich#15
Browse files Browse the repository at this point in the history
  • Loading branch information
abidkhan484 committed Oct 2, 2024
1 parent 01801ea commit 5f7a807
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
46 changes: 45 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ torchvision = [
{ version = "==0.17.2+cpu", source = "pytorch-cpu", markers = "sys_platform == 'linux' and platform_machine != 'aarch64'" }
]
tqdm = "^4.65.0"
rawpy = "^0.23.1"

[tool.poetry.group.dev.dependencies]
pycodestyle = ">=2.7,<3.0"
Expand Down
19 changes: 17 additions & 2 deletions rclip/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from tqdm import tqdm
import PIL
from PIL import Image, ImageFile
from rawpy import imread

from rclip import db, fs, model
from rclip.utils.preview import preview
Expand Down Expand Up @@ -41,7 +42,7 @@ def is_image_meta_equal(image: db.Image, meta: ImageMeta) -> bool:

class RClip:
EXCLUDE_DIRS_DEFAULT = ['@eaDir', 'node_modules', '.git']
IMAGE_REGEX = re.compile(r'^.+\.(jpe?g|png|webp)$', re.I)
IMAGE_REGEX = re.compile(r'^.+\.(jpe?g|png|webp|arw|dng|cr2)$', re.I)
DB_IMAGES_BEFORE_COMMIT = 50_000

class SearchResult(NamedTuple):
Expand All @@ -62,6 +63,17 @@ def __init__(
excluded_dirs = '|'.join(re.escape(dir) for dir in exclude_dirs or self.EXCLUDE_DIRS_DEFAULT)
self._exclude_dir_regex = re.compile(f'^.+\\{os.path.sep}({excluded_dirs})(\\{os.path.sep}.+)?$')

def _read_raw_file(self, path: str):
image = None
try:
raw = imread(path)
rgb = raw.postprocess()
image = Image.fromarray(np.array(rgb))
except Exception as ex:
print(f'not a valid raw file {path}', ex, file=sys.stderr)
return image


def _index_files(self, filepaths: List[str], metas: List[ImageMeta]):
images: List[Image.Image] = []
filtered_paths: List[str] = []
Expand All @@ -71,7 +83,10 @@ def _index_files(self, filepaths: List[str], metas: List[ImageMeta]):
images.append(image)
filtered_paths.append(path)
except PIL.UnidentifiedImageError as ex:
pass
image = self._read_raw_file(path)
if not image:
images.append(image)
filtered_paths.append(path)
except Exception as ex:
print(f'error loading image {path}:', ex, file=sys.stderr)

Expand Down

0 comments on commit 5f7a807

Please sign in to comment.