Skip to content

Instantly share code, notes, and snippets.

@kikoso
Created December 8, 2023 18:10
Show Gist options
  • Save kikoso/0f8e0d3a5fa5e7c8c4b5f2db073b2332 to your computer and use it in GitHub Desktop.
Save kikoso/0f8e0d3a5fa5e7c8c4b5f2db073b2332 to your computer and use it in GitHub Desktop.
Add Frame to Screenshot in Android
from PIL import Image
def overlay_images(background_path, overlay_path, output_path, padding=40, transparency=0, top_margin=30):
# Open background image
background = Image.open(background_path)
# Open overlay image
overlay = Image.open(overlay_path)
# Calculate the size for the padded background
padded_size = (background.width + 2 * padding, background.height + 2 * padding)
# Create a new image with transparent padding
padded_background = Image.new("RGBA", padded_size, (0, 0, 0, 0))
padded_background.paste(background, (padding, padding + top_margin)) # Adjusted for top_margin
# Resize overlay image to fit the padded background size
overlay = overlay.resize(padded_background.size, Image.LANCZOS)
# Prepare overlay with transparency
overlay_with_transparency = Image.new("RGBA", padded_background.size)
for x in range(overlay.width):
for y in range(overlay.height):
r, g, b, a = overlay.getpixel((x, y))
overlay_with_transparency.putpixel((x, y), (r, g, b, int(a * transparency)))
# Composite the images
combined = Image.alpha_composite(padded_background.convert("RGBA"), overlay_with_transparency)
# Save the result
combined.save(output_path, "PNG")
if __name__ == "__main__":
# Specify the paths to your images
background_image_path = "/Users/enriquelopezmanas/Desktop/background.png"
overlay_image_path = "/Users/enriquelopezmanas/Desktop/frame.png"
output_image_path = "/Users/enriquelopezmanas/Desktop/output.png"
# Overlay the images with padded background, transparent padding, and adjusted margins
overlay_images(background_image_path, overlay_image_path, output_image_path, padding=60, transparency=1.0,
top_margin=20)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment