|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Script to convert publication images to square format with white padding. |
| 4 | +Takes image filenames from the images/publications folder and saves them as square images. |
| 5 | +""" |
| 6 | + |
| 7 | +import os |
| 8 | +from PIL import Image, ImageOps |
| 9 | + |
| 10 | +# Input: List of image filenames from images/publications folder |
| 11 | +IMAGE_FILENAMES = [ |
| 12 | + "mocha.png", |
| 13 | + "purpcode.png", |
| 14 | + "sreasoner.png", |
| 15 | + "mtsbench.png", |
| 16 | + "hallusegbench.png", |
| 17 | + "part2gs.gif", |
| 18 | + "latteflow.png" |
| 19 | +] |
| 20 | + |
| 21 | +def make_square_with_padding(image_path, output_path): |
| 22 | + """ |
| 23 | + Convert an image to square format by adding white padding. |
| 24 | + |
| 25 | + Args: |
| 26 | + image_path (str): Path to the input image |
| 27 | + output_path (str): Path to save the square image |
| 28 | + """ |
| 29 | + try: |
| 30 | + # Open the image |
| 31 | + with Image.open(image_path) as img: |
| 32 | + # Convert to RGB if necessary (for PNG with transparency) |
| 33 | + if img.mode in ('RGBA', 'LA', 'P'): |
| 34 | + # Create a white background |
| 35 | + background = Image.new('RGB', img.size, (255, 255, 255)) |
| 36 | + if img.mode == 'P': |
| 37 | + img = img.convert('RGBA') |
| 38 | + background.paste(img, mask=img.split()[-1] if img.mode == 'RGBA' else None) |
| 39 | + img = background |
| 40 | + elif img.mode != 'RGB': |
| 41 | + img = img.convert('RGB') |
| 42 | + |
| 43 | + # Get current dimensions |
| 44 | + width, height = img.size |
| 45 | + |
| 46 | + # Determine the size of the square (max of width and height) |
| 47 | + max_size = max(width, height) |
| 48 | + |
| 49 | + # Create a new square image with white background |
| 50 | + square_img = Image.new('RGB', (max_size, max_size), (255, 255, 255)) |
| 51 | + |
| 52 | + # Calculate position to center the original image |
| 53 | + x_offset = (max_size - width) // 2 |
| 54 | + y_offset = (max_size - height) // 2 |
| 55 | + |
| 56 | + # Paste the original image onto the square canvas |
| 57 | + square_img.paste(img, (x_offset, y_offset)) |
| 58 | + |
| 59 | + # Save the square image |
| 60 | + square_img.save(output_path, quality=95, optimize=True) |
| 61 | + print(f"✓ Converted: {os.path.basename(image_path)} -> {os.path.basename(output_path)}") |
| 62 | + |
| 63 | + except Exception as e: |
| 64 | + print(f"✗ Error processing {image_path}: {str(e)}") |
| 65 | + |
| 66 | +def main(): |
| 67 | + """Main function to process all images in the list.""" |
| 68 | + # Define paths |
| 69 | + base_dir = os.path.dirname(os.path.abspath(__file__)) |
| 70 | + input_dir = os.path.join(base_dir, '..', "images", "publications") |
| 71 | + output_dir = os.path.join(base_dir, '..', "images", "publications") |
| 72 | + |
| 73 | + # Check if input directory exists |
| 74 | + if not os.path.exists(input_dir): |
| 75 | + print(f"Error: Input directory does not exist: {input_dir}") |
| 76 | + return |
| 77 | + |
| 78 | + # Create output directory if it doesn't exist |
| 79 | + os.makedirs(output_dir, exist_ok=True) |
| 80 | + |
| 81 | + print("Converting images to square format with white padding...") |
| 82 | + print("-" * 60) |
| 83 | + |
| 84 | + processed = 0 |
| 85 | + skipped = 0 |
| 86 | + |
| 87 | + for filename in IMAGE_FILENAMES: |
| 88 | + input_path = os.path.join(input_dir, filename) |
| 89 | + output_path = os.path.join(output_dir, filename) |
| 90 | + |
| 91 | + # Check if input file exists |
| 92 | + if not os.path.exists(input_path): |
| 93 | + print(f"⚠ Skipped: {filename} (file not found)") |
| 94 | + skipped += 1 |
| 95 | + continue |
| 96 | + |
| 97 | + # Process the image |
| 98 | + make_square_with_padding(input_path, output_path) |
| 99 | + processed += 1 |
| 100 | + |
| 101 | + print("-" * 60) |
| 102 | + print(f"Processing complete!") |
| 103 | + print(f"Processed: {processed} images") |
| 104 | + print(f"Skipped: {skipped} images") |
| 105 | + |
| 106 | +if __name__ == "__main__": |
| 107 | + main() |
0 commit comments