'Changing the values of R, G, B in the image takes a long time

I'm going to change the (r,g,b) values of the pixels in the image. I have 500 images with a size of 1048 x 2048 and there is 1 label for each image with the same size. The (r,g,b) values of each pixel of the labels are either (255,255,255) or (0,0,0). I check the (r,g,b) values of each pixel of each label and if it was (255, 255 ,255), I change the (r,g,b) values of the same pixel of the corresponding image. I wrote the code in two ways, but unfortunately it takes a long time to execute. I use cv2.imwrite at the end of the code or in each step, and it takes a long time in both cases. Did I make a mistake or is there a way to do it faster? Thanks.

Path = ''
searchlbl   = os.path.join( Path , "LABEL"   , "*" , "*" , "*_LBL.png" )
searchimg   = os.path.join( Path , "IMG"   , "*" , "*" , "*_IMG.png" )
imgFine = glob.glob( searchimg )
lblFine = glob.glob( searchlbl )
imgFine.sort()
lblFine.sort()
for i in range (0,500):
lbl = cv2.imread(lblFine[i])
img = cv2.imread(imgFine[i])
for m in range (0,1024):
    for n in range(0,2048):
        lbl_num=lbl[m,n]
        if (lbl_num[0] == 255):

            img_num=img[m,n]
            YKOM = img_num[0]

            if   (65<YKOM<75 ):
                img[m,n] = (0,0,0)
                #cv2.imwrite(imgFine[i], img)
                red = red + 1

            elif   (100<YKOM<110 ):
                img[m,n] = (0,0,0)
                #cv2.imwrite(imgFine[i], img)
                white = white + 1

            elif   (19<YKOM<28 ):
                img[m,n] = (0,0,0)
                #cv2.imwrite(imgFine[i], img)
                black = black + 1

            elif   (92<YKOM<99 ):
                img[m,n] = (0,0,0)
                #cv2.imwrite(imgFine[i], img)
                yellow = yellow + 1

            elif   (15<YKOM<20 ):
                img[m,n] = (0,0,0)
                #cv2.imwrite(imgFine[i], img)
                blue = blue + 1
            cv2.imwrite(imgFine[i], img)
        else:

            continue
print(i)   

I also changed the code this way:

for i in range (0,500):
lbl1 = Image.open(lblFine[i])
lbl = lbl1.load()
img1 = Image.open(imgFine[i])
img = img1.load()

for m in range (0,2048):
    for n in range(0,1024):
        lbl_num=lbl[m,n]
        if (lbl_num[0] == 255):

            img_num=img[m,n]
            YKOM = img_num[0]


            if   (65<YKOM<75 ):
                img[m,n] = (0,0,0)
                #cv2.imwrite(imgFine[i], img)

            elif   (100<YKOM<110 ):
                img[m,n] = (0,0,0)
                #cv2.imwrite(imgFine[i], img)

            elif   (19<YKOM<28 ):
                img[m,n] = (0,0,0)
                #cv2.imwrite(imgFine[i], img)

            elif   (92<YKOM<99 ):
                img[m,n] = (0,0,0)
                #cv2.imwrite(imgFine[i], img)

            elif   (15<YKOM<20 ):
                img[m,n] = (0,0,0)
                #cv2.imwrite(imgFine[i], img)

            img1.save(imgFine[i])
        else:

            continue
print(i)


Sources

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

Source: Stack Overflow

Solution Source