pythonimgur

How to upload an image to imgur using an image URL with python


I'm using the pyimgur module to upload to imgur. It works fine with local files but is there a way to upload to imgur using a image URL(jpg,png). I looked around but couldn't find a solid answer. Current code I'm using is this(for local images),

import pyimgur

CLIENT_ID = "<id>"
im = pyimgur.Imgur(CLIENT_ID)
image = '<local-image>'
uploaded_image = im.upload_image(image, title='title')

Thank you


Solution

  • import requests
    
    img_url = 'https://my/image/url.jpg'
    
    api = 'https://api.imgur.com/3/image'
    
    params = dict(
        client_id='546c25a59c58ad7'
    )
    
    files = dict(
        image=(None, img_url),
        name=(None, ''),
        type=(None, 'URL'),
    )
    
    r = requests.post(api, files=files, params=params)