-
-
Notifications
You must be signed in to change notification settings - Fork 329
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
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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"] | ||
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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. actually you should use |
||
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"], | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
|
@@ -119,3 +121,38 @@ def run_argparser(argparser): | |
raise Exception(f"\nError: Unknown arguments: {unknown}") | ||
|
||
return args | ||
|
||
|
||
def resize_image( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}") |
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 "$@" |
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 |
There was a problem hiding this comment.
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