Skip to content

WebNinjaDeveloper.com

Programming Tutorials




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

Python Script to Download Multiple Images From URL Using Urllib in Terminal

Posted on February 11, 2023

Welcome folks today in this blog post we will be downloading multiple images from urls in python using urllib. All the full source code of the application is shown below.

 

 

Get Started

 

 

In order to get started you need to make an app.py file and copy paste the following code

 

 

app.py

 

 

Python
1
2
3
4
5
6
7
8
9
10
import urllib.request
import os
from urllib.parse import urlparse
 
# List of image URLs
image_urls = [
    'https://images.unsplash.com/photo-1665686374221-1901faa9f3ad?ixlib=rb-4.0.3&ixid=MnwxMjA3fDF8MHxlZGl0b3JpYWwtZmVlZHwxMXx8fGVufDB8fHx8&auto=format&fit=crop&w=500&q=60',
    'https://images.unsplash.com/photo-1672350162550-f29c08e4c39a?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxlZGl0b3JpYWwtZmVlZHwyfHx8ZW58MHx8fHw%3D&auto=format&fit=crop&w=500&q=60',
    'https://images.unsplash.com/photo-1661956602868-6ae368943878?ixlib=rb-4.0.3&ixid=MnwxMjA3fDF8MHxlZGl0b3JpYWwtZmVlZHw2fHx8ZW58MHx8fHw%3D&auto=format&fit=crop&w=500&q=60',
]

 

 

As you can see we are importing the urllib library and from that we are importing the request module and also we are importing the os module for path and also we are importing the urlparse module. And then we have the array of image urls from which the images will be downloaded from the internet.

 

 

Python
1
2
3
# Create a directory to save the images
if not os.path.exists('images'):
    os.makedirs('images')

 

 

As you can see in the above code we are just creating the directory of images where all the images will get downloaded. Here we are just checking if the directory doesn’t exist then create the directory using the makedirs() method.

 

 

Python
1
2
3
4
5
6
# Download and save the images
for url in image_urls:
    response = urllib.request.urlopen(url)
    filename = os.path.join('images', os.path.basename(urlparse(url).path + ".png"))
    with open(filename, 'wb') as f:
        f.write(response.read())

 

 

And now in the above code we are looping through all the image urls present inside the array and for each url we are making a request using the urllib library and we are using the urlopen() method. And after that we are making the path of the image using the basename() method and inside that we are merging the path of the image and also the extension of the image which is .png image. And lastly we are saving the downloaded image file by opening it in write binary mode and then using the write() method to save the image file inside the images folder as shown below

 

 

 

Recent Posts

  • Node.js Tutorial to Export Images to PDF Document With Effects Using FilePix Library in Javascript
  • Node.js Tutorial to Export All Pages of PDF Document to Images and Save it in Javascript
  • Node.js OfficeGen Example to Add Text & Images in Powerpoint Presentation in Javascript
  • Node.js OfficeGen Example to Generate Excel Files By Adding Data inside Cells & Sheets in Javascript
  • Node.js OfficeGen Example to Create Word Docx Files & Add Text Images inside it Using Javascript
  • 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