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

1 Answer

0 votes
by (271k points)

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)


...