1 """An example to display both image and its corresponding color palette."""
2
3 import os
4
5 import cv2
6 import numpy as np
7
8 from gamebeye.gbcamcolors.color_helpers import hex_to_rgb
9 from gamebeye.gbcamimage.gbcamimage import GBCamImage
10
11 input_folder = os.path.join("images", "colorizedImages")
12 images_filepath = sorted(
13 [os.path.join(input_folder, filename) for filename in os.listdir(input_folder)]
14 )
15
16 for image_filepath in images_filepath:
17 print(f"Processing {image_filepath}")
18 gb_image = GBCamImage()
19
20 # Reading of the file
21 gb_image.read(image_filepath)
22
23 # Displaying the colorized image
24 image_title = "Colorized image with {} color palette".format(
25 gb_image.color_palette.name
26 )
27 cv2.imshow(image_title, gb_image.data)
28
29 # Create a numpy image with each color of the gb_image color palette
30 # Join them to have a 4-elements array to print the color palette
31 color_palettes = []
32 for color in gb_image.color_palette.value:
33 rgb_color = hex_to_rgb(color)
34 red = rgb_color[0] * np.ones((112, 128)).astype("uint8")
35 green = rgb_color[1] * np.ones((112, 128)).astype("uint8")
36 blue = rgb_color[2] * np.ones((112, 128)).astype("uint8")
37 color_palettes.append(cv2.merge([blue, green, red]))
38 color_palettes = cv2.hconcat(color_palettes)
39
40 color_palette_title = "Color palettes {}".format(gb_image.color_palette.name)
41 cv2.imshow(color_palette_title, color_palettes)
42
43 cv2.waitKey()
44 cv2.destroyWindow(image_title)
45 cv2.destroyWindow(color_palette_title)