Beginner: How to create ImageList from json file? (rename folders)

I’ve got a json file with this structure: {1, “daisy”, 2, “orchid”} etc. I’ve got folders named with numbers. For example, there is a folder named “1” and it contains images of daisies. The images themselves are named with uninformative integers like “123455.jpg”.

What is the code to rename the folders based on the json file? I’ve searched and tried different approaches (such as generating a dict with key/value pairs based on the json file), but continue to get various errors. I know this is probably very easy, but I just can’t get it working!

You can use datablock api and use get labels from function.
Here’s fragment of my code (i’m using label from .xml, but it should be pretty similar for json).

def get_label_from_xml(file_path):
    parts =
    file_name = parts[-1].replace('jpg', 'xml')
    root = ET.parse(str(path_img/file_name)).getroot()
    return root[7].text

src = (ImageList.from_folder(path_img)
        .use_partial_data(pct, seed) #comment out for learning

But instead of getting label this way you should open the json file and look up your folder name and then return that. If you need more help than that please let me know.

1 Like