Skip to content

Instantly share code, notes, and snippets.

@Te-k
Last active May 11, 2016 19:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Te-k/baa954a91c2174f1746f5dbd1e1eccea to your computer and use it in GitHub Desktop.
Save Te-k/baa954a91c2174f1746f5dbd1e1eccea to your computer and use it in GitHub Desktop.
Convert picture to 200 px width and upload it to lut.im for LQDN Press Review
#! /usr/bin/python2
import sys
import requests
import tempfile
from PIL import Image
def resize(source_path, dest_path):
"""Resize the image from the source path to the dest"""
png = Image.open(source_path)
width = png.size[0]
height = (png.size[1] * 200) / width
png.resize((200,height), Image.ANTIALIAS).save(dest_path)
def send_lutim(path):
"""Send the image given to lutim"""
url = 'https://lut.im/'
data = {'format' : 'json'}
files = {'file': open(path, 'rb')}
r = requests.post(url, files=files, data=data)
if r.status_code == 200:
data = r.json()
return True, url + data['msg']['short'], data['msg']['token']
else:
return False, "", ""
if __name__ == '__main__':
if len(sys.argv) < 2:
print("Bad arguments, you should give an image. Quitting...")
sys.exit(1)
f = tempfile.NamedTemporaryFile(delete=False, suffix=".png", prefix="lutim")
dst = f.name
resize(sys.argv[1], dst)
res, url, token = send_lutim(dst)
if res:
print("Successfully uplaoded to lut.im")
print("url: %s" % url)
print("Token: %s" % token)
print('Use this : <img src="%s" class="l" />' % url)
else:
print("Failed to uploaded to lut.im :'(")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment