Python Forum
Convert 400 grayscale pixels into RGB - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Convert 400 grayscale pixels into RGB (/thread-23492.html)



Convert 400 grayscale pixels into RGB - python420 - Jan-02-2020

I'm working on a data set from text files and I would like to:

1. Convert the dataset into an array in order to make .jpg images
2. Convert the grayscale into RBG
3. Convert dataset into 5000 jpg images (saved into laptop)
4. Use 1000 random RBG jpg images for the test data
5. Start using Keras for image classification using CNN, in order to classify images by its color.

So I'm stuck on step 2.
How could one convert 400 pixels values ranging from -1 to 1 into a RGB format? How could one encode this in hexadecimal or make it fit into a RGB triplets ?


RE: Convert 400 grayscale pixels into RGB - Clunk_Head - Jan-02-2020

Assuming -1 is black and 1 is white.
You have to add 1 to it to shift the range from 0 to 2.
Then divide by 2 to get the percentage.
Then multiply that by 256.
Then round that number.
Then convert that int to hex.
Then chop off the first two characters '0x'.
Then since RGB grey is the same code repeated three times, multiply the string by 3.
The tack a hashtag on the front and you have a complete RGB color code.
# tri_gray is the gray ranging from -1 to 1
'#' + hex(round((tri_gray + 1) / 2 * 256))[2:]*3