Advertisement
TankorSmash

Untitled

Sep 3rd, 2012
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.00 KB | None | 0 0
  1. from cStringIO import StringIO
  2. from PIL import Image, ImageGrab
  3. import requests
  4. import base64
  5. from pprint import pprint
  6. import time
  7.  
  8. #needs PIL, requests and optionally pyWin32
  9.  
  10. time_to_sleep = 3
  11. anon_key = r"f0a52082a31a6658ba4545c1c9fa4c68"
  12.  
  13.  
  14. print 'sleeping for {} seconds'.format(time_to_sleep)
  15. time.sleep(time_to_sleep)
  16.  
  17.  
  18. output = StringIO()
  19.  
  20. #grab the screen
  21. im = ImageGrab.grab()
  22. im.save(output, "BMP")
  23. im.save("test.png")
  24.  
  25. #grab the data from the image
  26. data = output.getvalue()[14:]
  27.  
  28. ### Uncomment this to send screenshot to clipboard. Needs pyWin32###
  29. ##send the data to clipboard
  30. #import win32clipboard
  31. #def send_to_clipboard(clip_type, data):
  32.     #win32clipboard.OpenClipboard()
  33.     #win32clipboard.EmptyClipboard()
  34.     #win32clipboard.SetClipboardData(clip_type, data)
  35.     #win32clipboard.CloseClipboard()
  36.  
  37. #send_to_clipboard(win32clipboard.CF_DIB, data)
  38. ### END UNCOMMENT ###
  39.  
  40. ##now to send it to imgur
  41.  
  42.  
  43.  
  44. #your api key will go here, as a string
  45. api_key = '5a5141ca9354bf7929b98a1d7a4c26ae'
  46.  
  47. #here's the API url that you'll need to POST to
  48. url = r'http://api.imgur.com/2/upload.json'
  49.  
  50. #full image path
  51. image_path = 'test.png'
  52.  
  53. #open binary data, instead of regular read
  54. f = open(image_path, 'rb')
  55.  
  56. #encode image file for transfer
  57. binary_data = f.read()  #again, not string data, but binary data
  58. b64image = base64.b64encode(binary_data)
  59.  
  60. #data to send with the POST request
  61. payload = {'key' : api_key,
  62.            'image': b64image,
  63.            'title': 'apitest',}  #title of image as seen on imgur.com
  64.  
  65. #make the POST request, with the attached data of payload
  66. print "uploading image"
  67. r = requests.post(url, data=payload)
  68.  
  69. #turn the returned json into a python dict
  70. #j = json.loads(r.text)
  71. j = r.json
  72.  
  73. #print it
  74. image_link = str(j['upload']['links']['original'])
  75. print "Your image is at: {}".format(image_link)
  76.  
  77. ## OPTIONAL attach imgur link to clipboard
  78. #send_to_clipboard(win32clipboard.CF_TEXT, image_link)
  79.  
  80. raw_input("Press any key to exit")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement