Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from cStringIO import StringIO
- from PIL import Image, ImageGrab
- import requests
- import base64
- from pprint import pprint
- import time
- #needs PIL, requests and optionally pyWin32
- time_to_sleep = 3
- anon_key = r"f0a52082a31a6658ba4545c1c9fa4c68"
- print 'sleeping for {} seconds'.format(time_to_sleep)
- time.sleep(time_to_sleep)
- output = StringIO()
- #grab the screen
- im = ImageGrab.grab()
- im.save(output, "BMP")
- im.save("test.png")
- #grab the data from the image
- data = output.getvalue()[14:]
- ### Uncomment this to send screenshot to clipboard. Needs pyWin32###
- ##send the data to clipboard
- #import win32clipboard
- #def send_to_clipboard(clip_type, data):
- #win32clipboard.OpenClipboard()
- #win32clipboard.EmptyClipboard()
- #win32clipboard.SetClipboardData(clip_type, data)
- #win32clipboard.CloseClipboard()
- #send_to_clipboard(win32clipboard.CF_DIB, data)
- ### END UNCOMMENT ###
- ##now to send it to imgur
- #your api key will go here, as a string
- api_key = '5a5141ca9354bf7929b98a1d7a4c26ae'
- #here's the API url that you'll need to POST to
- url = r'http://api.imgur.com/2/upload.json'
- #full image path
- image_path = 'test.png'
- #open binary data, instead of regular read
- f = open(image_path, 'rb')
- #encode image file for transfer
- binary_data = f.read() #again, not string data, but binary data
- b64image = base64.b64encode(binary_data)
- #data to send with the POST request
- payload = {'key' : api_key,
- 'image': b64image,
- 'title': 'apitest',} #title of image as seen on imgur.com
- #make the POST request, with the attached data of payload
- print "uploading image"
- r = requests.post(url, data=payload)
- #turn the returned json into a python dict
- #j = json.loads(r.text)
- j = r.json
- #print it
- image_link = str(j['upload']['links']['original'])
- print "Your image is at: {}".format(image_link)
- ## OPTIONAL attach imgur link to clipboard
- #send_to_clipboard(win32clipboard.CF_TEXT, image_link)
- raw_input("Press any key to exit")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement