0

My goal: convert each pixel of an image to hexadecimal code where every 6 symbols means color and save such code in a string. Then I would be able to detect, which color dominates in the picture.

How can I convert picture to 6 symbols hex code (where for example 000000 is black and FFFFFF is white)?

I tried this C# code:

using System.IO;
using System.Runtime.Remoting.Metadata.W3cXsd2001;
...
var str = new SoapHexBinary(File.ReadAllBytes("test.png")).ToString();
Console.WriteLine("str: {0}", str);

It gives we a string (I am pasting just a snip):

89504E470D0A1A0A0000000D4948445200000026000000210806000000B538D916000000017352474200AECE1CE90000000467414D410000B18F0BFC610

I want to interpret every 6 symbols as a color code but for example #89504E is dark pink and my test picture looks just like that:

enter image description here

How can I convert picture to 6 digits hex color code?

vytaute
  • 1,260
  • 4
  • 16
  • 36
  • 3
    You need to understand a file's format if you want to read it raw. Here you overlooked the __header__, but also the fact that png files will be __compressed__ etc. - The way to go is to load a Bitmap and loop over its pixels, counting them, maybe in a Dictionary>Color, long> - . Also: You should understand that 24bpp amount to 16mio colors and many will look indistiguishable. So you need a more lucid definition of 'dominating color' or simply throw away a or two lower bits from the colors.. – TaW Sep 14 '21 at 14:38
  • 1
    HEX is just a base-16 (0-9A-F) number system, instead of a base-10 number system (0-9). What you got is just a HEX representation of the byte array you passed, it has nothing to do with the colors inside the png. - maybe this helps: https://medium.com/@savas/why-do-we-use-hexadecimal-d6d80b56f026 – Rand Random Sep 14 '21 at 14:48
  • Maybe I can convert picture to lower quality first, with less bits like this picture: https://static.boredpanda.com/blog/wp-content/uploads/2020/06/low-quality-photo-face-depixelizer-28-5ef1f22d0b090__700.jpg and then it will be visible which color would go with picture, which color is dominating (not directly but just as an idea) – vytaute Sep 14 '21 at 14:49
  • @RandRandom okey but I need to convert picture to its colors array somehow – vytaute Sep 14 '21 at 14:50
  • 1
    As @TaW already said you will need to read each individual pixel eg. https://stackoverflow.com/questions/10127871/how-can-i-read-image-pixels-values-as-rgb-into-2d-array – Rand Random Sep 14 '21 at 14:51
  • 1
    Google brought this: https://stackoverflow.com/questions/30103425/find-dominant-color-in-an-image - did you check it? – Rand Random Sep 14 '21 at 14:53
  • Thank you all. I am trying out all these solutions now ;) – vytaute Sep 14 '21 at 14:57

0 Answers0