How To Download Image File From Url Use Python Requests Or Wget Module

This example will show you how to download an image file from an image url use python requests module. You will find this example code is simple and clear. Below example code can also download any web url file.

1. Download Url Image By Python Requests Module Steps.

  1. Open a terminal, and run command python or python3 to enter python interactive command console.
  2. Run below example code in above python interactive command console. The example image url is . After run below python code, it will download the image and save it to a local file local_image.jpg.
    # Import requests, shutil python module.
    import requests
    import shutil
    # This is the image url.
    image_url = ""
    # Open the url image, set stream to True, this will return the stream content.
    resp = requests.get(image_url, stream=True)
    # Open a local file with wb ( write binary ) permission.
    local_file = open('local_image.jpg', 'wb')
    # Set decode_content value to True, otherwise the downloaded image file's size will be zero.
    resp.raw.decode_content = True
    # Copy the response stream raw data to local image file.
    shutil.copyfileobj(resp.raw, local_file)
    # Remove the image url response object.
    del resp

2. Download Url Image By Python Wget Module Steps.

Besides python requests module, python wget module can also be used to download url image to local file easily. Below is the steps about how to use it.

  1. Open a terminal and run pip show wget to check whether python wget module has been installed or not.
  2. If python wget module has not been installed, then run pip install wget command in terminal to install it.
    $ pip install wget
    Collecting wget
    Installing collected packages: wget
      Running install for wget ... done
    Successfully installed wget-3.2
  3. Run python or python3 to enter python interactive command console.
  4. Run below python code in above python interactive command console.
    # First import wget python module.
    >>> import wget
    >>> image_url = ''
    # Invoke wget download method to download specified url image.
    >>> local_image_filename =
    100% [................................................] 829882 / 829882>
    # Print out local image file name.
    >> local_image_filename


  1. I tried using the firs approach (changed nothing) and it produced empty image (0kb)

    second approach produced error: HTTPError: Forbidden

    Do you know why is that? I am using python 3.7 and spyder IDE


    1. A bit late, It means you don’t have access to see that resource


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.