|
import os |
|
import numpy as np |
|
from PIL import Image, ImageOps |
|
|
|
|
|
def crop_and_pad_image(image_path, threshold=20, target_size=(512, 512)): |
|
""" |
|
Crop and pad an image to a square with the specified target size. |
|
|
|
Args: |
|
image_path (str): Path to the input image file. |
|
threshold (int): Threshold value for binarizing the image. |
|
target_size (tuple): Target size of the output image (width, height). |
|
|
|
Returns: |
|
PIL.Image.Image: Cropped and padded image. |
|
""" |
|
try: |
|
|
|
image = Image.open(image_path).convert("RGB") |
|
except Exception as e: |
|
raise ValueError(f"Error loading image: {str(e)}") |
|
|
|
|
|
image_array = np.array(image) |
|
|
|
|
|
binary_image_array = np.where(image_array > threshold, 1, 0).astype(np.uint8) |
|
|
|
|
|
non_zero_indices = np.argwhere(binary_image_array) |
|
|
|
|
|
if non_zero_indices.size == 0: |
|
raise ValueError(f"No non-zero elements found for the image: {image_path}") |
|
|
|
|
|
(y1, x1, _), (y2, x2, _) = non_zero_indices.min(0), non_zero_indices.max(0) |
|
|
|
|
|
cropped_img = image.crop((x1, y1, x2, y2)) |
|
|
|
|
|
squared_img = ImageOps.pad(cropped_img, target_size) |
|
|
|
return squared_img |
|
|
|
|
|
def track_files(folder_path, extensions=('.jpg', '.jpeg', '.png')): |
|
""" |
|
Track all the files in a folder and its subfolders. |
|
|
|
Args: |
|
folder_path (str): The path of the folder to track files in. |
|
extensions (tuple, optional): Tuple of file extensions to track. Default is ('.jpg', '.jpeg', '.png'). |
|
|
|
Returns: |
|
list: A list containing the paths of all files in the folder and its subfolders. |
|
""" |
|
|
|
if not os.path.isdir(folder_path): |
|
raise ValueError("Invalid folder path provided.") |
|
|
|
|
|
extensions = tuple(ext.lower() for ext in extensions) |
|
|
|
|
|
file_list = [] |
|
|
|
|
|
for root, dirs, files in os.walk(folder_path): |
|
for filename in files: |
|
file_path = os.path.join(root, filename) |
|
_, extension = os.path.splitext(file_path) |
|
|
|
if extension.lower() in extensions: |
|
file_list.append(file_path) |
|
|
|
return file_list |