Skip to content

Commit

Permalink
read image according to extension #15
Browse files Browse the repository at this point in the history
  • Loading branch information
abidkhan484 committed Oct 7, 2024
1 parent 097f9df commit f5a569c
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions rclip/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ 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):
def _read_raw_image_file(self, path: str):
image = None
try:
raw = imread(path)
Expand All @@ -73,22 +73,28 @@ def _read_raw_file(self, path: str):
print(f'not a valid raw file {path}', ex, file=sys.stderr)
return image

def _read_image_file(self, path: str):
image = None
try:
image = Image.open(path)
except PIL.UnidentifiedImageError as ex:
print(f'unidentified image error {path}:', ex, file=sys.stderr)
except Exception as ex:
print(f'error loading image {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] = []
for path in filepaths:
try:
image = Image.open(path)
if os.path.splitext(path)[1].lower() in fs.PRIORITIZED_IMAGE_EXTENSIONS:
image = self._read_image_file(path)
else:
image = self._read_raw_image_file(path)

if image:
images.append(image)
filtered_paths.append(path)
except PIL.UnidentifiedImageError as ex:
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)

try:
features = self._model.compute_image_features(images)
Expand Down

0 comments on commit f5a569c

Please sign in to comment.