from os import listdir
from os.path import isfile, join
from path import Path
import numpy as np
import cv2

# Dataset path
target_path = Path('target/')
annotation_images_path = Path('dataset/ade20k/annotations/training/').abspath()
dataset = [ f for f in listdir(annotation_images_path) if isfile(join(annotation_images_path,f))]
images = np.empty(len(dataset), dtype = object)
count = 1
# Iterate all Training Images
for n in range(0, len(dataset)):
    # Read image
    images[n] = cv2.imread(join(annotation_images_path,dataset[n]))

    # Convert it to array
    array = np.asarray(images[n],dtype=np.int8)

    # Conditions when the value equal less than 1, change it to 255. 
    # If it is >= 1, increment it by -1
    arr = np.where(array < 1, 255, array -1)

    #Saved it to another file 
    if count < 10:
        cv2.imwrite(target_path +'ADE_train_0000000'+ str(count) + ".png", arr)
    elif count < 100 and count > 9:
        cv2.imwrite(target_path +'ADE_train_000000'+ str(count) + ".png", arr)
    elif count < 1000 and count > 99:
        cv2.imwrite(target_path +'ADE_train_00000'+ str(count) + ".png", arr)
    elif count < 10000 and count > 999:
        cv2.imwrite(target_path +'ADE_train_0000'+ str(count) + ".png", arr)
    else:
        cv2.imwrite(target_path +'ADE_train_000'+ str(count) + ".png", arr)
    print(str(count) + ".png is printed")
    count += 1
    

                

[((8, 14, 8, 29), 'path.Path', 'Path', ({(8, 19, 8, 28): '"""target/"""'}, {}), "('target/')", False, 'from path import Path\n'), ((19, 12, 19, 47), 'numpy.asarray', 'np.asarray', (), '', True, 'import numpy as np\n'), ((23, 10, 23, 44), 'numpy.where', 'np.where', ({(23, 19, 23, 28): 'array < 1', (23, 30, 23, 33): '255', (23, 35, 23, 43): 'array - 1'}, {}), '(array < 1, 255, array - 1)', True, 'import numpy as np\n'), ((9, 25, 9, 69), 'path.Path', 'Path', ({(9, 30, 9, 68): '"""dataset/ade20k/annotations/training/"""'}, {}), "('dataset/ade20k/annotations/training/')", False, 'from path import Path\n'), ((10, 23, 10, 54), 'os.listdir', 'listdir', ({(10, 31, 10, 53): 'annotation_images_path'}, {}), '(annotation_images_path)', False, 'from os import listdir\n'), ((16, 27, 16, 66), 'os.path.join', 'join', ({(16, 32, 16, 54): 'annotation_images_path', (16, 55, 16, 65): 'dataset[n]'}, {}), '(annotation_images_path, dataset[n])', False, 'from os.path import isfile, join\n'), ((10, 65, 10, 95), 'os.path.join', 'join', ({(10, 70, 10, 92): 'annotation_images_path', (10, 93, 10, 94): 'f'}, {}), '(annotation_images_path, f)', False, 'from os.path import isfile, join\n')]