Created
February 22, 2025 11:51
-
-
Save deep5050/be911fe9eeba8ed38a3b189a90ffb3de to your computer and use it in GitHub Desktop.
Add border to images
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import argparse | |
| from PIL import Image, ImageDraw | |
| def add_borders(input_image_path, output_image_path, border_width=10, border_color="white", rounded_border=False, rounded_image=False): | |
| # Open the original image | |
| with Image.open(input_image_path) as img: | |
| # Calculate the new size | |
| new_size = (img.width + 2 * border_width, img.height + 2 * border_width) | |
| # Create a new image with the specified background color | |
| new_img = Image.new("RGB", new_size, border_color) | |
| # Create a mask for rounded corners for the border if needed | |
| if rounded_border: | |
| mask = Image.new("L", new_size, 0) | |
| draw = ImageDraw.Draw(mask) | |
| radius = border_width # Radius for rounded corners | |
| draw.rounded_rectangle([0, 0, new_size[0], new_size[1]], radius=radius, fill=255) | |
| new_img.putalpha(mask) | |
| # Create a mask for rounded corners for the original image if needed | |
| if rounded_image: | |
| img_mask = Image.new("L", img.size, 0) | |
| draw = ImageDraw.Draw(img_mask) | |
| draw.rounded_rectangle([0, 0, img.width, img.height], radius=border_width, fill=255) | |
| img = img.convert("RGBA") # Convert to RGBA to support transparency | |
| img.putalpha(img_mask) | |
| # Paste the original image onto the new image | |
| new_img.paste(img, (border_width, border_width), img if rounded_image else None) | |
| # Save the new image | |
| new_img.save(output_image_path) | |
| def main(): | |
| # Set up argument parser | |
| parser = argparse.ArgumentParser(description="Add borders to an image with optional rounded corners.") | |
| parser.add_argument("input_image", help="Path to the input image file.") | |
| parser.add_argument("output_image", help="Path to save the output image file.") | |
| parser.add_argument("--border_width", type=int, default=10, help="Width of the border in pixels (default: 10).") | |
| parser.add_argument("--border_color", type=str, default="white", help="Color of the border (default: white).") | |
| parser.add_argument("--rounded_border", action="store_true", help="Enable rounded corners for the border.") | |
| parser.add_argument("--rounded_image", action="store_true", help="Enable rounded corners for the image.") | |
| # Parse arguments | |
| args = parser.parse_args() | |
| # Call the function with the provided arguments | |
| add_borders(args.input_image, args.output_image, args.border_width, args.border_color, args.rounded_border, args.rounded_image) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment