Skip to content

Instantly share code, notes, and snippets.

@lolzballs
Created May 30, 2015 01:11
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lolzballs/a62b5cf5262fc7179dee to your computer and use it in GitHub Desktop.
Save lolzballs/a62b5cf5262fc7179dee to your computer and use it in GitHub Desktop.
Uploads images from clipboard to imgur and put the link in your clipboard
#!/usr/bin/python
import pygtk
pygtk.require('2.0')
import gtk
import sys, os
import cStringIO
import base64
import json
import requests
import subprocess
from base64 import b64encode
client_id = "0d7cfd59581bfcb"
client_secret = "0f8983c9973adbcee07171a91e4cc60a1a63b61a"
headers = {"Authorization": "Client-ID " + client_id}
url = "https://api.imgur.com/3/upload"
def upload(img):
fH = cStringIO.StringIO()
img.save_to_callback(fH.write, "png")
response = requests.post(url, headers = headers,
data = {
'key': client_secret,
'image': b64encode(fH.getvalue()),
'type': 'base64'
})
link = response.json()['data']['link']
return link
def getImage():
clipboard = gtk.clipboard_get()
img = clipboard.wait_for_image()
if img == None:
subprocess.Popen(['notify-send', 'No image in clipboard'])
sys.exit(0)
return img
link = upload(getImage())
print link
os.popen('xsel -i -b', 'wb').write(link)
subprocess.Popen(['notify-send', 'Image uploaded', '--hint=int:transient:1'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment