'How can i convert json file from labelme interface to png or image format file?

When i used the labeling the images from labelme interface as output i get json file.but i need to in image format like png,bmp,jpeg after labeling. can anyone suggest me any code ?

import json from PIL import Image

with open('your,json') as f:
    data = json.load(f)

    # Load the file path from  the json
    imgpath = data['yourkey']
    
    # Place the image path into the open method
    img = Image.open(imgpath)


Solution 1:[1]

Based on the tutorial of the original repository, you can use labelme_json_to_dataset <<JSON_PATH>> -o <<OUTPUT_FOLDER_PATH>>.

To run it on python / jupyter, you can use:

import os

def labelme_json_to_dataset(json_path):
    os.system("labelme_json_to_dataset "+json_path+" -o "+json_path.replace(".","_"))

If you need to do it for multiple images, just loop the function.


Based on the issue, labelme_json_to_dataset behavior can be reimplemented by using either labelme2voc.py or labelme2coco.py.


You also could use other implementation like labelme2Datasets


You also can implement your own modification of labelme_json_to_dataset using labelme library. Basically, you use label_file = labelme.LabelFile(filename=filename) followed by img = labelme.utils.img_data_to_arr(label_file.imageData). An example of a process would be like this:

import labelme

import os
import glob

def labelme2images(input_dir, output_dir, force=False, save_img=False, new_size=False):
    """
    new_size_width, new_size_height = new_size
    """
    if save_img:
        _makedirs(path=osp.join(output_dir, "images"), force=force)
        if new_size:
            new_size_width, new_size_height = new_size
    
    print("Generating dataset")
    
    filenames = glob.glob(osp.join(input_dir, "*.json"))
        
    for filename in filenames:
        # base name
        base = osp.splitext(osp.basename(filename))[0]

        label_file = labelme.LabelFile(filename=filename)

        img = labelme.utils.img_data_to_arr(label_file.imageData)
        h, w = img.shape[0], img.shape[1]

        if save_img:
            if new_size:
                img_pil = Image.fromarray(img).resize((new_size_height, new_size_width))
            else:
                img_pil = Image.fromarray(img)
                
            img_pil.save(osp.join(output_dir, "images", base + ".jpg"))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1