Skip to content

WebNinjaDeveloper.com

Programming Tutorials




Menu
  • Home
  • Youtube Channel
  • Official Blog
  • Nearby Places Finder
  • Direction Route Finder
  • Distance & Time Calculator
Menu

Python 3 Download Images From URL using Request,Wget & UrlLib Libraries Full Example

Posted on February 11, 2023

 

 

Welcome folks today in this blog post we will be downloading images from url using request,wget and urllib libraries. All the full source code of the application is shown below.

 

 

Get Started

 

 

In order to get started you need to install the below libraries using the below command as shown below

 

 

pip install requests

 

 

pip install wget

 

 

pip install urllib3

 

 

app.py

 

 

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
## Importing Necessary Modules
import requests # to get image from the web
import shutil # to save it locally
 
## Set up the image URL and filename
image_url = "https://codingdiksha.com/wp-content/uploads/2021/06/convert-json-to-excel-python.png"
filename = image_url.split("/")[-1]
 
# Open the url image, set stream to True, this will return the stream content.
r = requests.get(image_url, stream = True)
 
# Check if the image was retrieved successfully
if r.status_code == 200:
    # Set decode_content value to True, otherwise the downloaded image file's size will be zero.
    r.raw.decode_content = True
    
    # Open a local file with wb ( write binary ) permission.
    with open(filename,'wb') as f:
        shutil.copyfileobj(r.raw, f)
        
    print('Image sucessfully Downloaded: ',filename)
else:
    print('Image Couldn\'t be retreived')

 

 

As you can see we have provided the url for the image and then we are downloading it inside the local disk using the python code. Here we are importing the requests and the shutil library. And here first of all we are making the http requests to download the image file using the get() method. And then we are opening the image file using the open() method of the shutil library.

 

 

Downloading Image From URL Using Wget Library

 

 

app.py

 

 

Python
1
2
3
4
5
6
7
8
9
10
# First import wget python module.
import wget
 
# Set up the image URL
image_url = "https://codingdiksha.com/wp-content/uploads/2021/06/convert-json-to-excel-python.png"
 
# Use wget download method to download specified image url.
image_filename = wget.download(image_url)
 
print('Image Successfully Downloaded: ', image_filename)

 

 

As you can see we are importing the wget library and then we are having the image url and then we are using the download() method to actually fetch the image and saving the image inside the local disk.

 

 

Downloading Image From URL Using Urllib Library

 

 

app.py

 

 

Python
1
2
3
4
5
6
7
8
9
# importing required modules
import urllib.request
 
# setting filename and image URL
filename = 'codingdiksha.jpg'
image_url = "https://codingdiksha.com/wp-content/uploads/2021/06/convert-json-to-excel-python.png"
 
# calling urlretrieve function to get resource
urllib.request.urlretrieve(image_url, filename)

 

 

As you can see we are importing the urllib module at the top and then we are providing the custom file name and the url of the image file and then we are using the urlretrieve() method to download the image and saving it inside the local disk.

 

 

 

Recent Posts

  • Android Java Project to Build MP4 Video to MP3 Audio Converter Using MediaMuxer Class
  • Android Java Project to Merge Multiple PDF Documents From Gallery Using iTextPDF Library
  • Android Java Project to Detect System Hardware & System CPU Info & Display inside TextView Widget
  • Android Java Project to Integrate Google OAuth2 Login & Logout System & Save User Info in SharedPreferences
  • Android Java Project to Export Raw Text to PDF Document Using iTextPDF Library
  • Angular
  • Bunjs
  • C#
  • Deno
  • django
  • Electronjs
  • java
  • javascript
  • Koajs
  • kotlin
  • Laravel
  • meteorjs
  • Nestjs
  • Nextjs
  • Nodejs
  • PHP
  • Python
  • React
  • ReactNative
  • Svelte
  • Tutorials
  • Vuejs




©2023 WebNinjaDeveloper.com | Design: Newspaperly WordPress Theme