I am seeking a way to easily get a 1D palette image that is sorted by intensity (lowest on the left, highest on the right).
I am curious if there's a simple way to give imagemagick an input image and it will find all the colors, sort them, and then output a pixel for every color in a long horizontal row?
Possible?
Is it possible to *easily* create a nx1 png palette from an image?
-
- Posts: 7
- Joined: 2016-11-08T21:03:34-07:00
- Authentication code: 1151
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Is it possible to *easily* create a nx1 png palette from an image?
I'm not sure what you mean. Do you want the image sorted, or the palette sorted? I think you want the image sorted, whether or not it has a palette, like this:
Input:
Output:
My process module "sortpixels" does that, sorting according to the "-intensity" setting.
I also have a "sortpixelsblue" (not yet published) which sorts by the blue channel, then the green channel within that, then the red channel. In other words, it sorts by channel, but blue is the most significant. (I may rename it "sortpixelsbgr".)
Input:
Output:
My process module "sortpixels" does that, sorting according to the "-intensity" setting.
I also have a "sortpixelsblue" (not yet published) which sorts by the blue channel, then the green channel within that, then the red channel. In other words, it sorts by channel, but blue is the most significant. (I may rename it "sortpixelsbgr".)
snibgo's IM pages: im.snibgo.com
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Is it possible to *easily* create a nx1 png palette from an image?
What might be the purpose/use for such a sorted image?
-
- Posts: 7
- Joined: 2016-11-08T21:03:34-07:00
- Authentication code: 1151
Re: Is it possible to *easily* create a nx1 png palette from an image?
Sorry for not giving more context... I am making a video game that uses multiple (large) colored textures that are identical to each other except for their color.. So I have a blue, purple, red, green, orange, yellow, white version of all my texture files, which was really driving me crazy because it was so wasteful app-size-wise as well as memory-wise. I have been exploring ways to programmatically color the textures, and the technique I have decided to go with is to use a single grayscale image, and with OpenGL fragment shaders, colorize the image from a palette based on the original colored files.
The way this has to work is, the palette needs to be 1px in height, and "number of colors in the texture" pixels in width. If they are sorted from lowest intensity to highest intensity, then OpenGL can easily translate a grayscale pixel in intensity to it's matching colored version.
I have tried using Gimp to generate a color palette, but it is not really working well (sorting seems to be kind of buggy, and generating an image based off of the palette is not as easy as I would like it to be either).
So I am hoping for an ImageMagick solution that I can just give it a colorized file, and it will output a 1px high png image that has every color in the texture sorted from lowest intensity (on the left side) to brightest intensity (on the right side).
The way this has to work is, the palette needs to be 1px in height, and "number of colors in the texture" pixels in width. If they are sorted from lowest intensity to highest intensity, then OpenGL can easily translate a grayscale pixel in intensity to it's matching colored version.
I have tried using Gimp to generate a color palette, but it is not really working well (sorting seems to be kind of buggy, and generating an image based off of the palette is not as easy as I would like it to be either).
So I am hoping for an ImageMagick solution that I can just give it a colorized file, and it will output a 1px high png image that has every color in the texture sorted from lowest intensity (on the left side) to brightest intensity (on the right side).
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Is it possible to *easily* create a nx1 png palette from an image?
That's exactly what my module does.patrick99e99 wrote:... give it a colorized file, and it will output a 1px high png image that has every color in the texture sorted from lowest intensity (on the left side) to brightest intensity (on the right side).
But my modules need to be compiled. If you don't have many colours (eg <=256), you could have a script that writes pixels as text lines (intensity, red, green, blue), sort by intensity, and rebuild the image from just the RGB values.
snibgo's IM pages: im.snibgo.com
-
- Posts: 7
- Joined: 2016-11-08T21:03:34-07:00
- Authentication code: 1151
Re: Is it possible to *easily* create a nx1 png palette from an image?
Yeah, I just don't know anything about how to programmatically build image files. Can you give me an example of how to build an image from a text file?
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Is it possible to *easily* create a nx1 png palette from an image?
On unix systems, you can do the following:
convert input imaged to a histogram in text format and store each histogram intensity value in an Arr(ay) sorted by highest intensity first
Then us NetPGM to convert the array into a 1D grayscale (row) image and save as png.
So now what do you want to do with this 1D lookup table image?
convert input imaged to a histogram in text format and store each histogram intensity value in an Arr(ay) sorted by highest intensity first
Then us NetPGM to convert the array into a 1D grayscale (row) image and save as png.
Code: Select all
infile="logo:"
Arr=(`convert "$infile" -colorspace gray -depth 8 -format "%c" histogram:info: |\
tr -cs "0-9\n" " " | cut -d\ -f3 | sort -gr`)
num=${#Arr[*]}
echo "P2 $num 1 255\n ${Arr[*]}" | convert - -depth 8 lut.png
-
- Posts: 7
- Joined: 2016-11-08T21:03:34-07:00
- Authentication code: 1151
Re: Is it possible to *easily* create a nx1 png palette from an image?
Thank you very much for that code.. I'll try it out.
The plan is, in my game, I have a fragment shader program running which will have the grayscale texture as the primary texture, and a 2ndary texture will be the palette, and as the image is drawn, it will be sampling the color from the palette based off of intensity and drawing itself with the colors of the palette.
The plan is, in my game, I have a fragment shader program running which will have the grayscale texture as the primary texture, and a 2ndary texture will be the palette, and as the image is drawn, it will be sampling the color from the palette based off of intensity and drawing itself with the colors of the palette.
-
- Posts: 7
- Joined: 2016-11-08T21:03:34-07:00
- Authentication code: 1151
Re: Is it possible to *easily* create a nx1 png palette from an image?
So, how do I make that be non-grayscale? I need this 1D palette to be of the colors from the colored image, not the grayscale version.. I tried changing that flag to "-colorspace rgb", but saw no difference.