Python helper functions for image editing tasks.
Returns a list of image paths in a folder filtered by allowed file extensions.
By default, list_image_paths finds all supported image formats (jpg, jpeg, tif, tiff, png, nef, cr2, dng).
from pathlib import Path
from image_utils import list_image_paths
# Specify directory
image_directory = Path("path/to/folder/of/images")
# List all supported image paths
image_paths = list_image_paths(image_directory)
print(f"Found {len(image_paths)} images.")
# Iterate and process each image
for img_path in image_paths:
print(img_path.name)You can restrict the search to a single file type or a tuple of specific extensions using the allowed_extensions parameter.
jpg_images = list_image_paths(image_directory, allowed_extensions="jpg")raw_images = list_image_paths(image_directory, allowed_extensions=("cr2", "dng", "nef"))-
Path Input: Accepts both
pathlib.Pathobjects and strings. -
Default Supported Extensions: jpg, jpeg, tif, tiff, png, nef, cr2, and dng.
-
Non-recursive: Only files in the immediate directory are scanned (subdirectories are ignored).
Given the pixel dimension of the longest edge, returns the rescaled width and height of an image to keep aspect ratio.
from PIL import Image
from image_utils import rescale_width_height
# Open an image with PIL
img = Image.open("path/to/img.jpg")
# Retrieve the image's original dimensions
w, h = img.size
# Rescale the image's dimensions where size is the longest edge
scaled_wh = rescale_width_height(width=w, height=h, size=1000)
# Resize the image with new dimensions
resized_img = img.resize(scaled_wh, Image.Resampling.BICUBIC)import cv2
from image_utils import rescale_width_height
# Open an image with OpenCV
img = cv2.imread("path/to/img.jpg")
# Retrieve the image's original dimensions
h, w, _ = img.shape
# Rescale the image's dimensions where size is the longest edge
scaled_wh = rescale_width_height(width=w, height=h, size=1000)
# Resize the image with new dimensions
resized_img = cv2.resize(img, scaled_wh, cv2.INTER_AREA)