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
    

                

[((138, 153), 'path.Path', 'Path', (['"""target/"""'], {}), "('target/')\n", (142, 153), False, 'from path import Path\n'), ((577, 613), 'numpy.asarray', 'np.asarray', (['images[n]'], {'dtype': 'np.int8'}), '(images[n], dtype=np.int8)\n', (587, 613), True, 'import numpy as np\n'), ((734, 769), 'numpy.where', 'np.where', (['(array < 1)', '(255)', '(array - 1)'], {}), '(array < 1, 255, array - 1)\n', (742, 769), True, 'import numpy as np\n'), ((179, 223), 'path.Path', 'Path', (['"""dataset/ade20k/annotations/training/"""'], {}), "('dataset/ade20k/annotations/training/')\n", (183, 223), False, 'from path import Path\n'), ((257, 288), 'os.listdir', 'listdir', (['annotation_images_path'], {}), '(annotation_images_path)\n', (264, 288), False, 'from os import listdir\n'), ((497, 537), 'os.path.join', 'join', (['annotation_images_path', 'dataset[n]'], {}), '(annotation_images_path, dataset[n])\n', (501, 537), False, 'from os.path import isfile, join\n'), ((299, 330), 'os.path.join', 'join', (['annotation_images_path', 'f'], {}), '(annotation_images_path, f)\n', (303, 330), False, 'from os.path import isfile, join\n')]