File size: 1,895 Bytes
fcd8868
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import os
from datasets import load_dataset
from PIL import Image


OUTPUT_DIR = "data/images"
NUM_IMAGES = 1000


def _valid_image_count_and_next_index():

    valid = 0
    next_index = 0

    if not os.path.isdir(OUTPUT_DIR):
        return valid, next_index

    for name in sorted(os.listdir(OUTPUT_DIR)):
        base, ext = os.path.splitext(name)
        if ext.lower() not in {".jpg", ".jpeg"}:
            continue
        if not base.isdigit():
            continue

        idx = int(base)
        path = os.path.join(OUTPUT_DIR, name)
        try:
            with Image.open(path) as img:
                img.verify()
            valid += 1
            next_index = max(next_index, idx + 1)
        except Exception:
            # Ignore broken files; they will not be counted as valid samples.
            continue

    return valid, next_index


def download_images():

    os.makedirs(OUTPUT_DIR, exist_ok=True)

    count, next_index = _valid_image_count_and_next_index()

    if count >= NUM_IMAGES:
        print(f"already have {count} valid images in {OUTPUT_DIR}")
        return

    dataset = load_dataset(
        "obvtiger/unsplash-img",
        split="train",
        streaming=True
    )

    for item in dataset:

        try:
            img = item.get("image")
        except Exception:
            continue

        if not isinstance(img, Image.Image):
            continue

        try:
            img = img.convert("RGB")
            img.thumbnail((512, 512))
        except Exception:
            continue

        path = os.path.join(OUTPUT_DIR, f"{next_index:04d}.jpg")

        try:
            img.save(path)
        except Exception:
            continue

        count += 1
        next_index += 1

        print(f"saved {path} ({count}/{NUM_IMAGES})")

        if count >= NUM_IMAGES:
            break


if __name__ == "__main__":
    download_images()