Skip to content

Instantly share code, notes, and snippets.

@thomashollier
Last active February 10, 2024 02:48
Show Gist options
  • Save thomashollier/31517f32feb749f083b172ad61ba79f7 to your computer and use it in GitHub Desktop.
Save thomashollier/31517f32feb749f083b172ad61ba79f7 to your computer and use it in GitHub Desktop.
Fit image A into B with even border
# This takes an image with an arbitrary aspect ratio and figures out the thickness of the border
# necessary to fit that image into an arbitrarily sized page while preserving the image's aspect ratio
# AKA: I'd like to center a randomly sized image into a standard (or also randomly) sized frame
# AKA: I want to put this image into this frame but I want an even border and I don't want to crop my image
imageWidth = 1080
imageHeight = 1920
pageWidth = 24
pageHeight = 36
imageAR = imageHeight/imageWidth
borderThickness = ((pageHeight/imageAR-pageWidth)*imageAR)/(-imageAR + 1)
print( "Border Thickness:", borderThickness)
print("Image AR:", imageAR)
print("Confirm AR in page with border thickness:",(pageHeight-borderThickness)/(pageWidth-borderThickness))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment