Skip to content

Instantly share code, notes, and snippets.

@vhsu
Created May 18, 2024 22:23
Show Gist options
  • Save vhsu/8f5b359ce4026b537bc7e2c2eed29047 to your computer and use it in GitHub Desktop.
Save vhsu/8f5b359ce4026b537bc7e2c2eed29047 to your computer and use it in GitHub Desktop.
Add a blank white border to an image in python using PIL
from PIL import Image
def add_border(input_image_path, output_image_path, border_size=100):
# Open an existing image
img = Image.open(input_image_path)
# Get the size of the original image
width, height = img.size
# Make the new image size as the original size plus the border
new_width = width + 2 * border_size
new_height = height + 2 * border_size
# Create a new image with RGB mode, white color, and the new size
new_img = Image.new("RGB", (new_width, new_height), "white")
# Paste the original image into the new image at the border offset
new_img.paste(img, (border_size, border_size))
# Save the new image
new_img.save(output_image_path)
print(f"The output image is saved to: {output_image_path}")
# Using the function
input_image_path = "testimg.jpg"
output_image_path = "modified.jpg"
add_border(input_image_path, output_image_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment