omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Uploading an image with ImgurPython

    Pythonista
    4
    10
    6503
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • lachlantula
      lachlantula last edited by

      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

      1 Reply Last reply Reply Quote 0
      • JonB
        JonB last edited by

        Use get_image_data() on your asset, which returns a BytesIO. ImgurPython upload accepts 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.

        1 Reply Last reply Reply Quote 1
        • lachlantula
          lachlantula last edited by

          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 install method is outdated or something?

          1 Reply Last reply Reply Quote 0
          • omz
            omz last edited by

            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)
            
            1 Reply Last reply Reply Quote 1
            • Webmaster4o
              Webmaster4o last edited by

              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.

              1 Reply Last reply Reply Quote 0
              • omz
                omz last edited by

                @Webmaster4o PNG is also quite common; you might want to just use something like i.format.lower() for the file extension. Btw, using format as 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).

                Webmaster4o 1 Reply Last reply Reply Quote 1
                • lachlantula
                  lachlantula last edited by lachlantula

                  @omz I tried your method, but for some reason I get a TypeError saying 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 ¯\_(ツ)_/¯

                  1 Reply Last reply Reply Quote 0
                  • JonB
                    JonB last edited by

                    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.

                    lachlantula 1 Reply Last reply Reply Quote 0
                    • Webmaster4o
                      Webmaster4o @omz last edited by

                      @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 pyimgur though.

                      1 Reply Last reply Reply Quote 0
                      • lachlantula
                        lachlantula @JonB last edited by

                        @JonB Works well, thanks!

                        If people from the future see this, he's talking about the client variable you create at the top of your program - client = ImgurClient(client_id,client_secret)

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post
                        Powered by NodeBB Forums | Contributors