'Convert image pixel data to one big string
I am looking to take an input of an image then look through every pixel and see which of 60 colors in a tuple(which each have a character corresponding to it) it is closest to, then add that character of the color to a string.
#rgb values of each color with the character it is
RecRoomColors = [
(191, 74, 84, 'a'),
(179, 24, 45, 'b'),
(107, 11, 38, 'c'),
(113, 42, 56, 'd'),
(115, 60, 55, 'e'),
(169, 51, 39, 'f'),
(198, 86, 46, 'g'),
(195, 119, 81, 'h'),
(199, 184, 101, 'i'),
(200, 175, 46, 'j'),
(161, 92, 37, 'k'),
(119, 89, 61, 'l')
(59, 74, 51, 'm'),
(40, 70, 36, 'n'),
(99, 148, 43, 'o'),
(127, 159, 83, 'p'),
(96, 168, 118, 'q'),
(16, 93, 47, 'r'),
(4, 54, 42, 's'),
(43, 69, 60, 't'),
(46, 79, 84, 'u'),
(3, 74, 74, 'v'),
(13, 143, 133, 'w'),
(98, 187, 183, 'x'),
(96, 175, 200, 'y'),
(19, 156, 200, 'z'),
(6, 80, 114, '`'),
(45, 84, 105, '~'),
(44, 73, 117, '!'),
(4, 52, 124, '@'),
(25, 99, 194, '#'),
(96, 148, 206, '$'),
(148, 125, 205, '%'),
(74, 25, 191, '^'),
(43, 19, 117, '&'),
(79, 66, 118, '*'),
(81, 55, 92, '('),
(57, 24, 75, ')'),
(111, 60, 127, '_'),
(188, 137, 204, '-'),
(192, 111, 163, '+'),
(192, 42, 79, '='),
(119, 12, 68, '['),
(95, 49, 78, ']'),
(30, 17, 37, '{'),
(55, 27, 39, '}'),
(63, 38, 44, '¼'),
(115, 59, 44, '|'),
(172, 123, 91, ';'),
(130, 93, 75, ':'),
(82, 58, 58, '9'),
(34, 27, 43, '"'),
(113, 111, 116, '<'),
(140, 137, 141, '>'),
(167, 167, 169, ','),
(199, 199, 199, '.'),
(91, 94, 101, '?'),
(67, 70, 79, '/'),
(39, 42, 59, '1'),
(18, 20, 41, '2'),
]
Solution 1:[1]
You can use a dictionary where its keys will be the pixels and the values the characters. Your code would be:
RecRoomColors = {
(191, 74, 84): 'a',
(179, 24, 45): 'b',
(107, 11, 38): 'c',
... # you have to complete here the rest of the values
(18, 20, 41): '2',
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Palinuro |
