0

I want to render an array which contains a string value as a library attribute but Line number 3 throws an error.

color_maps = ['AUTUMN','BONE', 'COOL', 'HOT', 'HSV', 'JET', 'OCEAN', 'PINK', 'RAINBOW','SPRING', 'SUMMER', 'WINTER']
image = cv2.imread('hurricane katrina 1.png', cv2.IMREAD_GRAYSCALE)

image_color_map = cv2.applyColorMap(image, cv2."COLORMAP_" + color_maps[0])

How will I do that in Python?

alyssaeliyah
  • 2,214
  • 6
  • 33
  • 80
  • 2
    You cannot compose constant literal names by using string concatenation. Instead you should edit your list as: `color_maps = [cv2.COLORMAP_AUTUMN, cv2.COLORMAP_BONE...]` and then call the `applyColorMap` as: `image_color_map = cv2.applyColorMap(image, color_maps[0])` – ZdaR Apr 09 '18 at 13:28
  • @ZdaR --> still throws an error. – alyssaeliyah Apr 09 '18 at 13:33

1 Answers1

1

Use the getattr built-in function:

color_maps = ['AUTUMN','BONE', 'COOL', 'HOT', 'HSV', 'JET', 'OCEAN', 'PINK', 'RAINBOW','SPRING', 'SUMMER', 'WINTER']
image = cv2.imread('hurricane katrina 1.png', cv2.IMREAD_GRAYSCALE)

image_color_map = cv2.applyColorMap(image, getattr(cv2,"COLORMAP_" + color_maps[0]))
ma3oun
  • 3,681
  • 1
  • 21
  • 33