[Python] Is there a way to make this code run faster

Discussão em 'Python' iniciado por Stack, Abril 3, 2025.

  1. Stack

    Stack Membro Participativo

    How can I write this for loop in numpy to make it run faster I am trying to convert grayscale label having values 0-7 to a colored image with corresponding colors

    def label_img_to_color(img):
    label_to_color = {
    0: [0, 0,0],
    1: [244, 35,232],
    2: [ 70, 70, 70],
    3: [102,102,156],
    4: [190,153,153],
    5: [153,153,153],
    6: [250,170, 30],
    7: [220,220, 0],
    }

    img_height, img_width = img.shape

    img_color = np.zeros((img_height, img_width, 3))
    for row in range(img_height):
    for col in range(img_width):
    label = img[row, col]

    img_color[row, col] = np.array(label_to_color[label])

    return img_color

    Continue reading...

Compartilhe esta Página