Skip to content

Instantly share code, notes, and snippets.

@nomicode
Created December 27, 2021 05:17
Show Gist options
  • Save nomicode/9cbd6954b65979ece102bfac428e4c6a to your computer and use it in GitHub Desktop.
Save nomicode/9cbd6954b65979ece102bfac428e4c6a to your computer and use it in GitHub Desktop.
Paste a screenshot on top of a template image to produce an output image with a drop shadow
#!/usr/bin/env python3
# This script pastes the input image on top of the template image and writes out
# a new file. The reference template image includes a drop shadow.
#
# To get this script working on macOS, install the following dependencies:
#
# $ brew install pngquant
# $ python3 -m pip install Pillow pngquant
#
# Because this script was designed to work on macOS, the image sizes have been
# doubled to account for retina resolution.
#
# Input images must be exactly 2400 by 1400 pixels.
import sys
import pathlib
from PIL import Image
import pngquant
if len(sys.argv) != 3:
print(f"Usage: {sys.argv[0]} TEMPLATE_IMG INPUT_IMG")
sys.exit(1)
template_filename = sys.argv[1]
input_filename = sys.argv[2]
template_img = Image.open(template_filename)
input_img = Image.open(input_filename)
# Check template image is correct size
if template_img.width != 2560 or template_img.height != 1560:
print(f"ERROR: Incorrect template image size")
sys.exit(1)
# Check input image is correct size
if input_img.width != 2400 or input_img.height != 1400:
print(f"ERROR: Incorrect input image size")
sys.exit(1)
template_img.paste(input_img, (80, 80))
input_path = pathlib.Path(input_filename)
new_stem = f"{input_path.stem}-out"
output_path = input_path.with_stem(new_stem)
template_img.save(output_path, optimize=True)
print(f"Saved: {output_path}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment