Python Forum

Full Version: Download set of w3school test images
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Here's a simple script to download a complete set of test images.
It will create a directory directly below the script directory, named test_images
and load images there (if not already downloaded)
This location can be changed by modifying self.imagepath
import os
from pathlib import Path
import requests

class ImagePaths:
    def __init__(self):
        # Set directory anchor
        os.chdir(os.path.abspath(os.path.dirname(__file__)))
        self.homepath = Path('.')

        self.imagepath = self.homepath / 'test_images'
        self.imagepath.mkdir(exist_ok=True)
        self.image_url_list = {
            'BMP': 'https://www.w3.org/People/mimasa/test/imgformat/img/w3c_home.bmp',
            'GIF': 'https://www.w3.org/People/mimasa/test/imgformat/img/w3c_home.gif',
            'JPEG': 'https://www.w3.org/People/mimasa/test/imgformat/img/w3c_home.jpg',
            'PNG': 'https://www.w3.org/People/mimasa/test/imgformat/img/w3c_home.png',
            '256-color BMP': 'https://www.w3.org/People/mimasa/test/imgformat/img/w3c_home_256.bmp',
            '256-color GIF': 'https://www.w3.org/People/mimasa/test/imgformat/img/w3c_home_256.gif',
            '256-color JPEG': 'https://www.w3.org/People/mimasa/test/imgformat/img/w3c_home_256.jpg',
            '256-color PNG': 'https://www.w3.org/People/mimasa/test/imgformat/img/w3c_home_256.png',
            'Grayscale BMP': 'https://www.w3.org/People/mimasa/test/imgformat/img/w3c_home_gray.bmp',
            'Grayscale GIF': 'https://www.w3.org/People/mimasa/test/imgformat/img/w3c_home_gray.gif',
            'Grayscale JPEG': 'https://www.w3.org/People/mimasa/test/imgformat/img/w3c_home_gray.jpg',
            'Grayscale PNG': 'https://www.w3.org/People/mimasa/test/imgformat/img/w3c_home_2.bmp',
            'B/W BMP': 'https://www.w3.org/People/mimasa/test/imgformat/img/w3c_home_2.bmp',
            'B/W GIF': 'https://www.w3.org/People/mimasa/test/imgformat/img/w3c_home_2.gif',
            'B/W JPEG': 'https://www.w3.org/People/mimasa/test/imgformat/img/w3c_home_2.jpg',
            'B/W PNG': 'https://www.w3.org/People/mimasa/test/imgformat/img/w3c_home_2.png',
            'Animation GIF': 'https://www.w3.org/People/mimasa/test/imgformat/img/w3c_home_animation.gif',
            'MNG': 'https://www.w3.org/People/mimasa/test/imgformat/img/w3c_home_animation.mng'
        }

class GetTestImages:
    def __init__(self):
        self.ipath = ImagePaths()
        self.load_images()
    
    def load_images(self):
        for key, url in self.ipath.image_url_list.items():
            filename = self.ipath.imagepath / url.split('/')[-1]
            if not filename.exists():
                response = requests.get(url)
                if response.status_code == 200:
                    with filename.open('wb') as fp:
                        fp.write(response.content)
                else:
                    print(f'Problem downloading {filename.name}')

if __name__ == '__main__':
    gti = GetTestImages()