This worked for me. Thank you for the tip. @ptrblck
import sys
import os
from PIL import Image
import numpy as np


mask_dir = "input/"
new_mask_dir = "output/"
if not os.path.exists(new_mask_dir):
    os.mkdir(new_mask_dir)

for filename in os.scandir(mask_dir):
    if filename.path.endswith(".png"):
        image = Image.open(filename.path)
        mask = np.array(image)
        mask[mask==3] = 50, #Right_Rectus
        mask[mask==4] = 51, #Left_Rectus
        mask[mask==5] = 52, #Right_Psoas
        mask[mask==6] = 53, #Left_Psoas
        mask[mask==7] = 54, #Right_Erector
        mask[mask==8] = 55, #Left_Erector
        mask[mask<50] = 0,  #unlabeled
        mask[mask==50] = 1, #Right_Rectus
        mask[mask==51] = 2, #Left_Rectus
        mask[mask==52] = 3, #Right_Psoas
        mask[mask==53] = 4, #Left_Psoas
        mask[mask==54] = 5, #Right_Erector
        mask[mask==55] = 6  #Left_Erector
        
        masked_img = Image.fromarray(mask)
        new_img_path = os.path.join(new_mask_dir, filename.name)
        masked_img.save(new_img_path)