Hey there!
So, today I'll be showing you how to upload images to Imgur using Python. For this, I will be using Python 2.7, but it won't be particularly hard to port to Python 3. I might do a followup showing how to do that if anyone requests it.
I should also note that for this, we will only be using Python modules that are part of the standard library. This makes the code a bit longer, but hopefully just as readable.
I guess your first step is to go over to api.imgur.com and register an application. You will need the "Client ID" value for "anonymous" image uploads.
First things first, we are going to need to import some python modules. Open a text editor, and add these imports.
import urllib2
import urllib
import base64
import json
Next, for uploading our image, we open our image file and read in its contents.
f = open("image.png", "rb") # open our image file as read only in binary mode
image_data = f.read() # read in our image file
This creates a variable named "image data", containing our raw image. Now, while the API documentation says we can just upload raw images, I prefer to Base64 encode them, so we do the following.
b64_image = base64.standard_b64encode(image_data)
Next up, set up your "Client ID" variable, and create a dictionary with the correct kind of authorization header.
client_id = "REDACTED" # put your client ID here
headers = {'Authorization': 'Client-ID ' + client_id}
Now, we need to create the data to go in our POST request. The following will do...
data = {'image': b64_image, 'title': 'test'} # create a dictionary.
Next, we simply fire off our request using urllib. This uploads the image.
request = urllib2.Request(url="https://api.imgur.com/3/upload.json", data=urllib.urlencode(data),headers=headers)
response = urllib2.urlopen(request).read()
The "response" object contains a JSON string. We need to parse it, so we use the JSON module to extract the link to our image.
parse = json.loads(response)
print parse['data']['link']
And there we go! We have successfully uploaded an image to Imgur using Python! I'll put out a followup later today with how we can automatically create whole albums this way, by automatically uploading entire directories!
You may find some of my other programming posts interesting!
Clypdown: Tool for grabbing music from clyp.it
Nice. I'm trying to figure out how to do this with images uploaded to steemitimages.com. But there doesn't seem to be much info available.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
I'll have to take a look at that, see if I can figure it out and write a simple uploader script :D
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Awesome. Be sure to post about it if you're successful. Looking forward to it.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
If you find a method it will be a pleasure to be informed :-)
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit