Python Forum
Convert 400 grayscale pixels into RGB
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert 400 grayscale pixels into RGB
#1
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 ?
Reply
#2
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  identify not white pixels in bmp flash77 17 2,503 Nov-10-2023, 09:21 PM
Last Post: flash77
  Converting an RGBA image to Grayscale and Binary Joni_Engr 3 4,649 Aug-23-2021, 11:54 AM
Last Post: Pedroski55
  Maximas in grayscale image yliats 2 2,027 Aug-13-2020, 01:11 PM
Last Post: yliats
  pillow reversing the order of pixels after every row johnEmScott 4 3,132 May-27-2020, 09:42 AM
Last Post: scidam
  python opencv grayscale conversion error Spandora 1 9,582 May-26-2019, 10:43 AM
Last Post: heiner55
  How to extract temperature value of pixels of a thermal image recorded by IR camera hamacvan 1 14,460 Jan-13-2019, 06:06 PM
Last Post: Larz60+
  show grayscale image using matplotlib zyb1003 1 16,527 Nov-01-2017, 11:45 AM
Last Post: heiner55
  Grayscale - plot python 1 3,183 Aug-10-2017, 11:56 AM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020