convert an image from sRGB to indexed. Color palette is ~400 colors, final image to use a max of 70 colors - linux

What I want to do:
I would like to change an image to indexed, provide the color map, and set the max number of colors in the image to 70 of the provided 400. I would like this final image to be exported in some kind of text based format.
What I have so far:
I have the color palette as a text list of ~400 HEX values and their RGB equivalents in an excel spreadsheet. I can create a csv or tab-separated file of whatever of the two formats are needed (hex or RGB).Using imagemagik, the -remap argument will do the palette conversion, and the -color argument to limit the number of colors to 70. If the indexed image is saved as bmp, I can easily import a BMP into c++/python/matlab/octave and do some operations on the array and write that to text file.
Where I'm stuck:
I'm struggling to efficiently get my text-based list of ascii values into a nice colormap image that I can feed to the -remap argument. I know I could manually create one by painstakingly creating an image and making one pixel each color in the colormap, but there must be a better way!
Other Stuff:
If you have any other advice about the process I've mentioned above or any suggestions as to how I can do this better/faster/more efficiently, I'm all ears!

Related

How do I compare the quality of Gifs and PNG using colors. Does calculating the bits per pixel work

Since gif uses 8 bit color dept and png uses 24, I can notice the difference between the two picture.
I want to find the way in which i can compare the colors of two images not by looking but with calculated datas.
What I have done till now is that I calculated the BPP of both gif and png image assuming that would be the best option to compare these two format.
I'm not sure if finding the bpp will give me the absolute color difference or if it is even the correct way.
Although a gif has maximum of 256 colors, it would still have high quality as long as the original "raw" image has less than 256 colors (Imaging a cartoon picture that only used 8 colors, no shading/blending etc). In your question you mentioned you could notice the difference between the two picture, then I assume you are talking about some "raw" image that has more than 256 colors, such as a natural photograph with the sky color gradually changing.
In this case, I think you might check the histogram of the images. For the gif image it will have at most 256 entries in the histogram; for the higher quality png image, it should have a histogram that has a matching shape (peaks and valleys), but more than 256 non-zero entries. If this is true you can almost be certain the png has a higher quality (assuming they are indeed the same picture).
You may even be able to further find out how the gif reduced the number of entries by combining several neighboring entries in the png's histogram into one entry.

Remove all but certain colors (color range) in an image?

I have a grayscale image, which has a certain wanted pattern of colours + some unwanted colors.
The colors I want to leave are (rgb):
000
666
121212
181818
242424
303030
363636
424242
As you can see, there's a pattern. Umfortunately there are some really "hard to select" colors also. The colors are actually contours (curves for height changes of the terrain), so they would be really tiresome to edit manually. The image is 2048 x 2048 pixels.
How could I somehow limit the image just to have those certain colors and maybe even automagically convert the wrong ones to those wanted ones?
You could try converting the image mode to indexed color:
Then choose a custom pallet:
Then add your custom colors to the pallet. Set dither to none. It should convert all colors to the index color they are closest to.
To remove the white lines... convert mode back to rgb, use magic wand to select all white lines (uncheck contiguous and set tolerance to around 10). Then choose select>modify>expand and expand selection by 1 pixel. Now choose edit>fill and choose content-aware.
This should fill in the lines with the colors around them.
Set color to index again to clean up the blured shape edges.
Hope this helps.

Mapping RGB/hex color codes to general color categories

Is there a dataset that maps each of the ~16M RGB or hex color values to a general color family/category - e.g. red, purple, orange, beige, brown, etc. - that I could access programmatically or load into a database or JSON document to cross-refence the color codes against? The use case is to classify the results of PIL color detection of swatch files into a small set of color pickers for a shopping site. It would also work if the mapping is a bit more granular, say 100-200 categories, since it would be easy enough to map those to my target 10-15 myself. I have some knowledge of kNN classification and will work with that if I have to, but it would be so much easier to use a static mapping if one already exists.
You can use a table such as the one in X11
http://www.astrouw.edu.pl/~jskowron/colors-x11/rgb.html
In order to find color proximity, it's best to transform the colors to Lab color space first, so that euclidean distances have more meaning, and then nearest neighbor would give good results.
You could convert from RGB to CIE Lab color space wherein Euclidian distance between two color selections is perceptually more meaningful. Here is the link to all relevant color space transformation formulae used in OpenCV's color conversion method (cvtColor): http://docs.opencv.org/modules/imgproc/doc/miscellaneous_transformations.html
Since your use case is to compare two swatches, I would advise you to use texture descriptors (http://www.robots.ox.ac.uk/~vgg/research/texclass/with.html) in addition to color information for better results.

Creating spreadsheet with OOXML & RGB color shows as hex 4 value instead of 3

I am creating a spreadsheet using OOXML. In the example spreadsheet I am referencing to compare my output versus Excel 2010's Output I see the following entry in styles.xml:
<font><sz val="11"/><color rgb="FFFF0000"/><name val="Calibri"/><family val="2"/><scheme val="minor"/></font>
My understanding of RGB colors is that they are usually represented as 3 numbers i.e. 255,0,0 or as Hex FF0000
But in the example spreadsheet I have it shows as rgb=FFFF0000 which I would read as 255,255,0,0
How is FFFF0000 derived from the color Red?
It appears that a FF is inserted in front of the RGB hex value in all my colors, but I would prefer to know if this is part of the OOXML specifications before I assume something and later have issues with an incorrect assumption.
Thanks
After some more research I have been able to answer my own question.
(Google ARGB for more info)
The 1st hex character (additional one preceding the RGB hex value) is used to define the level of transparency.
This Hex value ranges from X'00' which means fully transparent to X'FF" means fully opaque

Why is the bpp information at 0x1C in this .bmp image wrong?

Address 1D indicates the image is eight bits per pixel, but it isn't, each pixel is represented by 3 bytes (24 bits).
At first, I thought photoshop did this in error, but I found that this format was used for all greyscale images.
Instead of using four bytes for pixel, why don't .bmp images use a value from 0 - FF to describe the greyscale value of each pixel?
EDIT: I was able to answer my own question about the file structure
from Wikipedia
The 8-bit per pixel (8bpp) format
supports 256 distinct colors and
stores 1 pixel per 1 byte.
Each byte
is an index into a table of up to 256
colors. This Color Table is in 32bpp 8.8.8.0.8 RGBAX format.
The color table shown in the hex editor is four bytes per pixel.
Far below that is the actual pixel array, which is 8 bits per pixel.
I figured that out by a calculation, the image is 64 x 64, 4096 pixels.
The pixel array starts at 436, and ends at 1437. In decimal, the difference between those two numbers is 4097, so the pixel array is exactly one byte per pixel.
I am still curious as to why a color table is necessary for a greyscale image, though
I am still curious as to why a color table is necessary for a greyscale image, though
It looks like bmp files have no special greyscale mode. So you cannot set in the header the format is greyscale, so you need the color table to define the colors you use. Even if all colors are greyscale.
If you look at the .png format, you can define that you are using a greyscale image, so you don't need a color table there. (but it would also be possible to use a color table to create a grayscale image).

Resources