Home < How to Download Images in Bulk in Python

How to Download Images in Bulk in Python

Posted on: July 14, 2024

Bulk image downloading can be incredibly useful for various applications such as data analysis, machine learning, and web scraping. Python, with its rich ecosystem of libraries, makes it relatively straightforward to automate this process. In this article, we’ll walk through how to download images in bulk using Python, with sample code to illustrate the process.

Prerequisites

Before we dive in, ensure you have Python installed on your system. You can download it from python.org. Additionally, you will need the following Python libraries:

  • requests: For making HTTP requests.
  • os: For interacting with the operating system (creating directories, saving files, etc.).
  • BeautifulSoup: For parsing HTML content (if scraping from web pages).

You can install these libraries using pip:

pip install requests beautifulsoup4

Step-by-Step Guide

Step 1: Import Libraries

First, import the necessary libraries:

import requests
from bs4 import BeautifulSoup
import os
Step 2: Define the URLs and the Download Function

Define the list of URLs from which you want to download images and create a function to handle the downloading process:


def download_image(url, folder_path):
    if not os.path.exists(folder_path):
        os.makedirs(folder_path)
    
    response = requests.get(url)
    if response.status_code == 200:
        image_name = os.path.join(folder_path, url.split('/')[-1])
        with open(image_name, 'wb') as file:
            file.write(response.content)
    else:
        print(f"Failed to download image from {url}")

image_urls = [
    "https://m.media-amazon.com/images/I/71gCUFuulfL.__AC_SX300_SY300_QL70_FMwebp_.jpg",
    "https://m.media-amazon.com/images/I/71GbBJxYzWL._AC_SX679_.jpg",
    "https://m.media-amazon.com/images/I/71PbUDYkhIL._AC_SX679_.jpg"
]

folder_path = "downloaded_images"

for url in image_urls:
    download_image(url, folder_path)
 
If you need to download images from a web page, you can scrape the image URLs first. Crawl RSS feeds in python this can help to crawl the data in bulk and gather the image urls.
 

Conclusion

Downloading images in bulk using Python is a powerful technique that can save time and effort in various projects. By combining the requests and BeautifulSoup libraries, you can automate the process of scraping and downloading images from the web. This guide provides a basic framework, which you can expand upon based on your specific requirements. Happy coding!

Find a right dataset that you are looking for from crawl feeds store.

Datasets

Submit data request if not able to find right dataset.
Custom request