Uploading an image with ImgurPython
So I'm just starting to learn the Photos module in Pythonista and am currently working on a little program that lets you choose a photo from your Camera Roll and upload it to imgur using the official ImgurPython module. Installed with StaSh in case anybody's wondering.
Here's a bit of what I've got...
from imgurpython import ImgurClient client_id = 'my id' client_secret = 'my secret' client = ImgurClient(client_id, client_secret) # ... @ui.in_background def upload(sender): global img ImgurClient.upload_from_path(image path goes here, config = None, anon = True) @ui.in_background def findimage(sender): global img img1 = photos.pick_asset(assets=None, title='', multi=False) img2 = img1.get_ui_image() img = img1.get_image() v['imgview'].image = img2
The issue is in the upload function where we need to insert an image path, which is obviously tricky in Pythonista due to how iOS works. Anybody have ideas on some workarounds?
Sorry for using globals btw, I'm still learning :p
Use get_image_data() on your asset, which returns a BytesIO. ImgurPython
uploadaccepts a File-like object such as BytesIO -- no path needed.
Alternatively, you could have written the image to a file, but this is not needed.
Your first method doesn't seem to work for me; I'm just getting an
AttributeError: type object 'ImgurClient' has no attribute 'upload'.I checked the GitHub code for ImgurPython and it looks like it should work, but it isn't. Maybe the
pip installmethod is outdated or something?
I don't have experience with
ImgurPython, but if you do end up needing a file path, that is actually not a problem at all on iOS. Example:
asset = #... with open('temp.jpg', 'wb') as f: f.write(asset.get_image_data().read()) # ... ImgurClient.upload_from_path('temp.jpg', config=None, anon=True)
I have a snippet for doing this in Pyimgur:
import pyimgur,photos,clipboard,os,console i=photos.pick_image() format = 'gif' if (i.format == 'GIF') else 'jpg' i.save('img.'+format) clipboard.set(pyimgur.Imgur("303d632d723a549").upload_image('img.'+format, title="Uploaded-Image").link) console.hud_alert("link copied!") os.remove('img.'+format)
It's sort of densely written, but you may be able to get some help from it.
@Webmaster4o PNG is also quite common; you might want to just use something like
i.format.lower()for the file extension. Btw, using
formatas a variable name can bite you, as it's also a built-in function (though it's not that commonly used, so usually not really a problem).
@omz I tried your method, but for some reason I get a
TypeErrorsaying that's it's
missing 1 required positional argument: 'path'.Reading over the documentation I'm still pretty confused why I'm getting this because it seems like we're doing everything it's asking for ¯\_(ツ)_/¯
You need an actual client instance, not the type ImgurClient.
It does seem github is more up to date. You could clone it or download it.
@omz I wrote this more than year ago and really only spent 5 minutes on it. I may update it soon to try and remove the dependency on
@JonB Works well, thanks!
If people from the future see this, he's talking about the
clientvariable you create at the top of your program -
client = ImgurClient(client_id,client_secret)