File size: 765 Bytes
875ae89
 
 
1454eef
875ae89
 
1454eef
875ae89
 
 
 
 
1454eef
 
875ae89
 
1454eef
 
 
 
875ae89
 
 
 
1454eef
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import os
from datasets import load_dataset
from PIL import Image
import random

def download_images(name="food101"):
    dataset = load_dataset(name, split="train")

    # Create a directory to save the images if it doesn't exist
    output_dir = "data/pictures"
    os.makedirs(output_dir, exist_ok=True)

    # Limit to 500 images
    num_images = 500
    count = 0

    # Randomly select and save the images
    random_indices = random.sample(range(len(dataset)), num_images)
    for index in random_indices:
        example = dataset[index]
        image = example['image']
        image.save(os.path.join(output_dir, f"image_{count}.jpg"))  # Save as JPG
        count += 1

    print(f"Downloaded and saved {num_images} images to the folder '{output_dir}'")