Skip to content

Instantly share code, notes, and snippets.

@catter1
Created March 15, 2022 21:29
Show Gist options
  • Save catter1/ce647a39818800b4f563fae20d01ffd3 to your computer and use it in GitHub Desktop.
Save catter1/ce647a39818800b4f563fae20d01ffd3 to your computer and use it in GitHub Desktop.
Image Standardizer
import sys
from wand.image import Image
sys.argv.pop(0)
if len(sys.argv) < 1:
print("Please provide at least 1 image file path as an argument.")
sys.exit()
HEIGHT = 1008
WIDTH = 1792
RATIO = 0.5625
for imgpath in sys.argv:
image = Image(filename=imgpath)
#print(image.width, image.height, sep="x")
#print("Ratio:", image.height/image.width)
if RATIO >= image.height/image.width :
rswidth = (int)((HEIGHT * image.width) / image.height)
image.sample(rswidth, HEIGHT)
if image.width != WIDTH:
image.crop(width=WIDTH, height=HEIGHT, gravity="center")
else:
rsheight = (int)((WIDTH * image.height) / image.width)
image.sample(WIDTH, rsheight)
if image.height != HEIGHT:
image.crop(width=WIDTH, height=HEIGHT, gravity="center")
#print(image.width, image.height, sep="x")
#print("Ratio:", image.height/image.width)
image.save(filename=imgpath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment