Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a bulk resize image function #224

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 46 additions & 11 deletions scripts/local/resize_images.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,46 @@
# --replace flag (false by default, overwrites if same ext, deletes if diff ext)
# Print number of files found
# Confirmation before overwrite
# Input directory
# Output directory

# Skip corrupt images and print their path in output
# --trigger-size [0]
# --max-width
# --max-height
# --outputExtension ()
import os
from utils.bulk_ops_common import (
get_local_argparser,
run_argparser,
walk_and_extract_files,
resize_image,
)


def resize_images(input_dir, output_dir, max_width, max_height, trigger_size):
"""
Resize images in the input directory and save them in the output directory.

:param input_dir: Directory containing images to be resized.
:param output_dir: Directory to save resized images.
:param max_width: Maximum width for resizing.
:param max_height: Maximum height for resizing.
:param trigger_size: Minimum file size in bytes to trigger resizing.
"""
# Extract image files from the input directory
file_extensions = ["jpg", "jpeg", "png", "tiff"]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's move this as an cli arg, and add this as the default value. Also add a validation that only a subset of these 4 values is supported

files = walk_and_extract_files(input_dir, file_extensions)

# Ensure the output directory exists
if not os.path.exists(output_dir):
os.makedirs(output_dir)

# Resize each image and save it to the output directory
for file_path in files:
output_file_path = os.path.join(output_dir, os.path.basename(file_path))
resize_image(file_path, output_file_path, max_width, max_height, trigger_size)


if __name__ == "__main__":
# Set up argument parser and parse arguments
argparser = get_local_argparser()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually you should use add_common_args(...) which internally uses the get_local_argparser().

args = run_argparser(argparser)

# Call the resize_images function with parsed arguments
resize_images(
input_dir=args["input"],
output_dir=args["output"],
max_width=args["max_width"],
max_height=args["max_height"],
trigger_size=args["trigger_size"],
)
37 changes: 37 additions & 0 deletions scripts/local/utils/bulk_ops_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import glob
import operator
import os
from PIL import Image, UnidentifiedImageError
from src.utils.file import PathUtils

from src.utils.file import PathUtils

Expand Down Expand Up @@ -119,3 +121,38 @@ def run_argparser(argparser):
raise Exception(f"\nError: Unknown arguments: {unknown}")

return args


def resize_image(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's move this function into image_utils.py instead

input_path, output_path, max_width=None, max_height=None, trigger_size=0
):
"""
Resize an image based on given width, height, and trigger size.

:param input_path: Path to the input image.
:param output_path: Path to save the resized image.
:param max_width: Maximum width for resizing.
:param max_height: Maximum height for resizing.
:param trigger_size: Minimum file size in bytes to trigger resizing.
"""
try:
if os.path.getsize(input_path) > trigger_size:
with Image.open(input_path) as img:
width, height = img.size

# Resize based on max width or height while keeping the aspect ratio
if max_width and width > max_width:
new_height = int((max_width / width) * height)
img = img.resize((max_width, new_height), Image.ANTIALIAS)

if max_height and height > max_height:
new_width = int((max_height / height) * width)
img = img.resize((new_width, max_height), Image.ANTIALIAS)
Comment on lines +144 to +150
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I already have a util created for this in image_utils.py, can you check if we can use that?


# Save the resized image
img.save(output_path)
print(f"Resized image saved at: {output_path}")
except UnidentifiedImageError:
print(f"Skipping corrupt image: {input_path}")
except Exception as e:
print(f"Error resizing image {input_path}: {e}")
2 changes: 1 addition & 1 deletion scripts/run_all_tests_and_coverage.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
#!/usr/bin/env sh
# Note: we need this run in a file because currently the 'language: system'
# type in pre-commit-config doesn't seem to support multiple commands (even on using &&)
coverage run --source src -m pytest -rfpsxEX --disable-warnings --verbose --durations=3
Expand Down
2 changes: 1 addition & 1 deletion scripts/run_python_hook.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/sh
#!/usr/bin/env sh
echo "PYTHONPATH=$PWD:$PYTHONPATH python3 $@"
PYTHONPATH="$PWD:$PYTHONPATH" python3 "$@"
2 changes: 1 addition & 1 deletion scripts/run_single_test.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/bin/sh
#!/usr/bin/env sh
python3 -m pytest -rfpsxEX --disable-warnings -vv -k test_run_omr_marker_mobile