Create an image using GIMP by drawing different colors using the PENCIL.
{source_image : Source Image ? Image Template to Use}
For each part of your image that has a different color, define a value. You can update the table directly or upload a new table.
You may need to reorder the sequence of parts in the table in order to match the color of the part in your uploaded image. Check the color histogram below for the sequence.
{part_table : Part Values ? Values to Represent in the Image using Colors}
Then, choose a color map to use for representing the values.
{color_map_options : Color Map ? Color Map to Use for Representing the Values}
Finally, choose a color that will represent the value zero. You can use standard HTML color names or hex codes.
{zero_color_name_text : Zero Color ? Color to Use for Representing the Value Zero}
# CrossCompute
source_image_path = 'body-template-20210702.png'
part_table_path = 'parts.csv'
color_map_options_path = 'colormaps.txt'
zero_color_name_text = 'gainsboro'
target_folder = '/tmp'
from PIL import Image
image = Image.open(source_image_path)
image.mode
image
import numpy as np
image_array = np.array(image)
image_array.shape
Next, let's reshape image_array
into a list of rgb colors. Since image_array
has 3 dimensions and we are giving reshape()
two arguments, we are telling reshape()
to turn a 3 dimensional array into a 2 dimensional array. The -1
in the first argument tells reshape()
to determine the size of the first dimension automatically based on the sizes of the other dimensions.
# https://stackoverflow.com/a/48904991
color_array = image_array.reshape(-1, image_array.shape[2])
color_array.shape
# Count the number of unique colors
colors, counts = np.unique(color_array, axis=0, return_counts=True)
colors.shape
colors.reshape(-1, *colors.shape).shape
%matplotlib inline
import matplotlib.pyplot as plt
plt.imshow(colors.reshape(-1, *colors.shape))
plt.show()
Here is the distribution of colors in our image. As you can see, the white background dominates.
from os.path import join
color_histogram_image_path = join(target_folder, 'color_histogram.png')
figure = plt.figure()
plt.bar(range(len(colors)), counts, color=colors/256, edgecolor='black')
plt.xlabel('Part Index')
plt.ylabel('Pixel Count')
figure.savefig(color_histogram_image_path)
print('color_histogram_image_path = ' + color_histogram_image_path)
from pandas import read_csv
part_table = read_csv(part_table_path)
part_table
Finally, we replace the dummy colors with real colors using a customized color map.
color_map_name = open(color_map_options_path, 'rt').readlines()[0].strip()
color_map_name
from matplotlib import cm
color_map = cm.get_cmap(color_map_name)
np.array(color_map(0)) * 256
(np.array(color_map(0)) * 256)[:3]
image_array.T.shape
red_band, green_band, blue_band = image_array.T
red_band.shape
color = colors[0]
red_value, green_value, blue_value = color
color_area = (red_band == red_value) & (green_band == green_value) & (blue_band == blue_value)
color_area.T.shape
image_array.shape
old_image_array = image_array
new_image_array = old_image_array.copy()
new_image_array[color_area.T] = (np.array(color_map(0.1)) * 256)[:3]
Image.fromarray(old_image_array)
Image.fromarray(new_image_array)
from matplotlib.colors import to_rgb
np.array(to_rgb(zero_color_name_text)) * 256
# https://stackoverflow.com/a/3753428/192092
new_image_array = old_image_array.copy()
r_band, g_band, b_band = old_image_array.T
maximum_score = part_table.score.max()
for (part_index, part_row), color in zip(part_table.iterrows(), colors):
score = part_row.score
normalized_score = score / maximum_score
r_value, g_value, b_value = color
color_area = (r_band == r_value) & (g_band == g_value) & (b_band == b_value)
if normalized_score:
new_color = (np.array(color_map(normalized_score)) * 256)[:3]
else:
new_color = np.array(to_rgb(zero_color_name_text)) * 256
new_image_array[color_area.T] = new_color
new_image = Image.fromarray(new_image_array)
new_image
from os.path import join
target_image_path = join(target_folder, 'choropleth.png')
new_image.save(target_image_path)
print('target_image_path = ' + target_image_path)
{color_histogram_image : Color Histogram ? Color Distribution in Your Original Image}
{target_image : Generated Choropleth ? Your Color Coded Image}