Home > database >  Saving dictionary of images to local machine
Saving dictionary of images to local machine

Time:07-10

I have a dictionary of images and class labels:

{dog:[' dog1.jpg', 'dog2.jpg', 'dog3.jpg',],
cat:['cat1.jpg', 'cat2.jpg'...etc
]}

How do I save these locally on my windows machine in folders based on the dictionary keys ?

ie) have a 'dog' folder with all the dog images, a 'cat' folder with all cat images etc..

CodePudding user response:

Does this work for you?

import os

data = {
    "dog": ["dog1.jpg", "dog2.jpg", "dog3.jpg"],
    "cat": ["cat1.jpg", "cat2.jpg"]
}

for folder_name, images in data.items():
    dest_path = f"some_path/{folder_name}"
    os.mkdir(dest_path)
    for image in images:
        os.rename(f"some_path2/{image}", f"{dest_path}/{image}")

I assumed you have all the images in some directory and want to separate them by putting dog images into a dog directory etc.

  • Related