Format code style in leann_multi_vector.py for better readability
🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -97,21 +97,23 @@ def _natural_sort_key(name: str) -> int:
|
|||||||
return int(m.group()) if m else 0
|
return int(m.group()) if m else 0
|
||||||
|
|
||||||
|
|
||||||
def _load_images_from_dir(pages_dir: str, recursive: bool = False) -> tuple[list[str], list[Image.Image]]:
|
def _load_images_from_dir(
|
||||||
|
pages_dir: str, recursive: bool = False
|
||||||
|
) -> tuple[list[str], list[Image.Image]]:
|
||||||
"""
|
"""
|
||||||
Load images from a directory.
|
Load images from a directory.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
pages_dir: Directory path containing images
|
pages_dir: Directory path containing images
|
||||||
recursive: If True, recursively search subdirectories (default: False)
|
recursive: If True, recursively search subdirectories (default: False)
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Tuple of (filepaths, images)
|
Tuple of (filepaths, images)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Supported image extensions
|
# Supported image extensions
|
||||||
extensions = ("*.png", "*.jpg", "*.jpeg", "*.PNG", "*.JPG", "*.JPEG", "*.webp", "*.WEBP")
|
extensions = ("*.png", "*.jpg", "*.jpeg", "*.PNG", "*.JPG", "*.JPEG", "*.webp", "*.WEBP")
|
||||||
|
|
||||||
if recursive:
|
if recursive:
|
||||||
# Recursive search
|
# Recursive search
|
||||||
filepaths = []
|
filepaths = []
|
||||||
@@ -124,31 +126,33 @@ def _load_images_from_dir(pages_dir: str, recursive: bool = False) -> tuple[list
|
|||||||
for ext in extensions:
|
for ext in extensions:
|
||||||
pattern = os.path.join(pages_dir, ext)
|
pattern = os.path.join(pages_dir, ext)
|
||||||
filepaths.extend(glob.glob(pattern))
|
filepaths.extend(glob.glob(pattern))
|
||||||
|
|
||||||
# Sort files naturally
|
# Sort files naturally
|
||||||
filepaths = sorted(filepaths, key=lambda x: _natural_sort_key(os.path.basename(x)))
|
filepaths = sorted(filepaths, key=lambda x: _natural_sort_key(os.path.basename(x)))
|
||||||
|
|
||||||
# Load images with error handling
|
# Load images with error handling
|
||||||
images = []
|
images = []
|
||||||
valid_filepaths = []
|
valid_filepaths = []
|
||||||
failed_count = 0
|
failed_count = 0
|
||||||
|
|
||||||
for filepath in filepaths:
|
for filepath in filepaths:
|
||||||
try:
|
try:
|
||||||
img = Image.open(filepath)
|
img = Image.open(filepath)
|
||||||
# Convert to RGB if necessary (handles RGBA, P, etc.)
|
# Convert to RGB if necessary (handles RGBA, P, etc.)
|
||||||
if img.mode != 'RGB':
|
if img.mode != "RGB":
|
||||||
img = img.convert('RGB')
|
img = img.convert("RGB")
|
||||||
images.append(img)
|
images.append(img)
|
||||||
valid_filepaths.append(filepath)
|
valid_filepaths.append(filepath)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
failed_count += 1
|
failed_count += 1
|
||||||
print(f"Warning: Failed to load image {filepath}: {e}")
|
print(f"Warning: Failed to load image {filepath}: {e}")
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if failed_count > 0:
|
if failed_count > 0:
|
||||||
print(f"Warning: Failed to load {failed_count} image(s) out of {len(filepaths)} total files")
|
print(
|
||||||
|
f"Warning: Failed to load {failed_count} image(s) out of {len(filepaths)} total files"
|
||||||
|
)
|
||||||
|
|
||||||
return valid_filepaths, images
|
return valid_filepaths, images
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user