Created
March 31, 2021 00:00
-
-
Save symisc/345795cb5e2cfe5543c1e0ea6146a137 to your computer and use it in GitHub Desktop.
Check if a given image is of the right dimension and if not try to resize it using the PixLab API - https://pixlab.io/cmd?id=smartresize
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import json | |
# Check if a given image is of the right size: 800x600 and if not try to resize it. | |
# The command of interest here are header: https://pixlab.io/#/cmd?id=header & smartresize: https://pixlab.io/#/cmd?id=smartresize | |
img = 'https://s-media-cache-ak0.pinimg.com/736x/60/aa/e4/60aae45858ab6ce9dc5b33cc2e69baf7.jpg' | |
key = 'PIXLAB_API_KEY' # Your PixLab API Key - Get yours from https://pixlab.io/dashboard | |
# Obtain image metadata at first via header | |
req = requests.get('https://api.pixlab.io/header',params={'img':img,'key':key}) | |
reply = req.json() | |
if reply['status'] != 200: | |
print (reply['error']) | |
exit() | |
w = reply['width'] | |
h = reply['height'] | |
if w > 800 or h > 600: | |
print("Resizing image from "+str(w)+"x"+str(h)+" to near 800x600...") | |
# Invoke smart resize... | |
req = requests.get('https://api.pixlab.io/smartresize',params={'img':img,'key':key,'width':800,'height':600}) | |
reply = req.json() | |
if reply['status'] != 200: | |
print (reply['error']) | |
else: | |
print("Resized image: "+reply['link']) | |
else: | |
print("Uploaded image is of the correct size!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment