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

1 Answer

+1 vote
by (354k 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


...