+4 votes
in Programming Languages by (73.8k points)
How can I download an online image file from a web link using Python script?

1 Answer

+1 vote
by (348k points)
selected by
 
Best answer

You can use Python's requests module to fetch the contents of an image file from a web link.

Here is an example:

import requests

def save_image_file(ilink):
    response = requests.get(ilink)
    filename = ilink.split('/')[-1]
    if response.status_code == 200:
        with open(filename, 'wb') as f:
            f.write(response.content)
    else:
        print("Bad response code for:", ilink)

if __name__ == "__main__":
    url = 'URL_FOR_THE_IMAGE_FILE'
    save_image_file(url)

Related questions


...