From 96291019cd8162bef9a57e1ef5bbdf414e36fcb4 Mon Sep 17 00:00:00 2001 From: tiennm99 Date: Fri, 30 Jan 2026 10:30:21 +0700 Subject: [PATCH] feat: init --- image-resizer/.claude/settings.local.json | 8 ++ image-resizer/main.py | 98 +++++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 image-resizer/.claude/settings.local.json create mode 100644 image-resizer/main.py diff --git a/image-resizer/.claude/settings.local.json b/image-resizer/.claude/settings.local.json new file mode 100644 index 0000000..b6ef994 --- /dev/null +++ b/image-resizer/.claude/settings.local.json @@ -0,0 +1,8 @@ +{ + "permissions": { + "allow": [ + "mcp__acp__Write", + "mcp__acp__Edit" + ] + } +} diff --git a/image-resizer/main.py b/image-resizer/main.py new file mode 100644 index 0000000..e33b925 --- /dev/null +++ b/image-resizer/main.py @@ -0,0 +1,98 @@ +import os +from pathlib import Path + +from PIL import Image + + +def resize_image(input_path, output_path, scale_percent): + """ + Resize an image to a specified percentage of its original size. + + Args: + input_path: Path to the input image + output_path: Path where the resized image will be saved + scale_percent: Percentage to scale the image (e.g., 50 for 50%) + """ + try: + with Image.open(input_path) as img: + # Calculate new dimensions + width, height = img.size + new_width = int(width * scale_percent / 100) + new_height = int(height * scale_percent / 100) + + # Resize using high-quality resampling + resized_img = img.resize((new_width, new_height), Image.Resampling.LANCZOS) + + # Save the resized image + resized_img.save(output_path) + print(f"Resized: {input_path} -> {output_path}") + print(f" Original: {width}x{height}, New: {new_width}x{new_height}") + except Exception as e: + print(f"Error processing {input_path}: {e}") + + +def resize_images_in_folder( + folder_path, scale_percent, output_folder=None, overwrite=False +): + """ + Resize all images in a folder and its subfolders. + + Args: + folder_path: Path to the folder containing images + scale_percent: Percentage to scale the images (e.g., 50 for 50%) + output_folder: Optional output folder. If None, creates a 'resized' subfolder + overwrite: If True, overwrites original images. If False, saves to output folder + """ + # Supported image formats + image_extensions = {".jpg", ".jpeg", ".png", ".gif", ".bmp", ".tiff", ".webp"} + + # Setup output path + folder_path = Path(folder_path) + if overwrite: + output_base_path = folder_path + else: + if output_folder is None: + output_folder = folder_path / "resized" + else: + output_folder = Path(output_folder) + + # Process all files in folder and subfolders + for root, dirs, files in os.walk(folder_path): + root_path = Path(root) + + # Create relative path structure for output + if not overwrite: + relative_path = root_path.relative_to(folder_path) + current_output_path = output_folder / relative_path + current_output_path.mkdir(parents=True, exist_ok=True) + else: + current_output_path = root_path + + # Process each image file + for file in files: + file_path = root_path / file + file_ext = file_path.suffix.lower() + + if file_ext in image_extensions: + if overwrite: + output_path = file_path + else: + output_path = current_output_path / file + + resize_image(file_path, output_path, scale_percent) + + +if __name__ == "__main__": + # Configuration + FOLDER_PATH = "D:\\kvtm\\kvtm\\client\\res\\common\\ui\\EventTet2026" # Current directory - change to your folder path + SCALE_PERCENT = 50 # Resize to 50% of original size + + print(f"Image Resizer - Scaling to {SCALE_PERCENT}%") + print(f"Source folder: {FOLDER_PATH}") + print("Mode: Overwriting original images") + print("-" * 50) + + resize_images_in_folder(FOLDER_PATH, SCALE_PERCENT, overwrite=True) + + print("-" * 50) + print("Done!")