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 https://www.dev2qa.com/demo/images/green_button.jpg . 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 = "https://www.dev2qa.com/demo/images/green_button.jpg"
    
    # 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
      Downloading https://files.pythonhosted.org/packages/47/6a/62e288da7bcda82b935ff0c6cfe542970f04e29c756b0e147251b2fb251f/wget-3.2.zip
    Installing collected packages: wget
      Running setup.py 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 = 'https://www.dev2qa.com/demo/images/green_button.jpg'
    
    # Invoke wget download method to download specified url image.
    >>> local_image_filename = wget.download(image_url)
    100% [................................................] 829882 / 829882>
    
    # Print out local image file name.
    >> local_image_filename
    'green_button.jpg'
READ :   How To Fix Django: TemplateSyntaxError: Could Not Parse The Remainder Error In Template File

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.