Home > Article > Backend Development > How to Automate Image Downloading with Python: A Comprehensive Guide
In today’s digital age, managing and processing images programmatically can save you a significant amount of time and effort. If you’re looking to automate the process of downloading images from the web, you’ve come to the right place. In this article, we’ll dive into a Python script that does just that using the Pexels API — a popular source of high-quality stock photos.
Overview
The Python script provided here allows you to search for images on Pexels based on a query, download them if they meet certain criteria, and save them to your local system. The script uses several powerful libraries and APIs, including requests, Pillow, and Pexels API.
Key Features of the Script
API Integration: Fetch images from Pexels using their API.
Dynamic Filtering: Select images based on their dimensions and orientation.
Date Simulation: Though Pexels API does not directly support date-based filtering, the script simulates this by generating a random date.
Rate Limiting: Avoid hitting API rate limits with randomized delays.
Understanding the Code
Imports and Configuration
import os import requests from PIL import Image from io import BytesIO import random import time from datetime import datetime, timedelta
The script starts with importing essential libraries. Here’s a quick rundown:
API_KEY = '...' # Replace with your actual Pexels API key BASE_URL = 'https://api.pexels.com/v1/search' OUTPUT_DIR = 'output' SEARCH_QUERY = 'Subaru Forester' MIN_WIDTH = 1080 MIN_HEIGHT = 1920 NUM_IMAGES = 16 ORIENTATION = 'vertical'
Set up your API key, base URL for Pexels, and specify other parameters like search query, minimum image dimensions, number of images to download, and image orientation.
Ensuring Output Directory Exists
if not os.path.exists(OUTPUT_DIR): os.makedirs(OUTPUT_DIR)
The script checks if the output directory exists and creates it if necessary.
Fetching Images
def get_images(query, page=1, per_page=15): params = { 'query': query, 'page': page, 'per_page': per_page } response = requests.get(BASE_URL, headers=headers, params=params) response.raise_for_status() return response.json()
This function sends a request to the Pexels API to fetch images based on the search query and other parameters.
Downloading and Saving Images
def download_and_save_image(url, filename): response = requests.get(url) response.raise_for_status() img = Image.open(BytesIO(response.content)) if img.width >= MIN_WIDTH and img.height >= MIN_HEIGHT: if ORIENTATION == 'vertical' and (img.height / img.width > 1.4): img.save(filename) print(f'Saved {filename}') return 1 elif ORIENTATION == 'horizontal' and (img.width / img.height > 1.4): img.save(filename) print(f'Saved {filename}') return 1 return 0
This function handles downloading an image from the URL, checking its dimensions and orientation, and saving it if it meets the criteria.
Generating Random Dates
def generate_random_date(): end_date = datetime.now() start_date = end_date - timedelta(days=4*365) random_date = start_date + timedelta(days=random.randint(0, 4*365)) return random_date
This function generates a random date within the past four years. While Pexels API doesn’t provide date-based filtering directly, this random date simulates such a filter.
Main Function
def main(): images_downloaded = 0 page = 1 seen_images = set() target_date = generate_random_date() print(f"Target date for filtering: {target_date.strftime('%Y-%m-%d')}") while images_downloaded < NUM_IMAGES: data = get_images(SEARCH_QUERY, page) photos = data.get('photos', []) if not photos: print('No more photos found.') break random.shuffle(photos) # Shuffle to get a more random selection for photo in photos: if images_downloaded >= NUM_IMAGES: break image_url = photo['src']['original'] photo_date = datetime.strptime(photo['created_at'], '%Y-%m-%dT%H:%M:%S%z') if 'created_at' in photo else datetime.now() if photo_date > target_date and image_url not in seen_images: filename = os.path.join(OUTPUT_DIR, f'image_{images_downloaded + 1}.jpg') if download_and_save_image(image_url, filename): images_downloaded += 1 seen_images.add(image_url) page += 1 time.sleep(random.uniform(1, 2)) # Sleep to avoid hitting API rate limits
The main() function coordinates the image fetching, downloading, and saving process. It manages pagination, checks for duplicate images, and enforces delays to respect the API’s rate limits.
Running the Script
To run this script, make sure you have replaced API_KEY with your actual Pexels API key and adjusted other parameters as needed. Save the script in a .py file and execute it using a Python interpreter. The images that meet the specified criteria will be saved in the output directory.
Conclusion
Automating image downloads can streamline your workflow and ensure you have high-quality images at your disposal. This Python script provides a solid foundation for such tasks, with flexibility for customization based on your needs. Whether you’re working on a personal project or integrating image downloading into a larger application, this script demonstrates the power and versatility of Python in handling web-based tasks.
The above is the detailed content of How to Automate Image Downloading with Python: A Comprehensive Guide. For more information, please follow other related articles on the PHP Chinese website!