Page 1 of 2
Getting the color of a pixel
Posted: 2018-09-19T00:38:16-07:00
by dholcomb9711
I am working with weather data - this is "super resolution" doppler wind velocity data. My data exporter writes geo-TIFs and colorizes them, but sometimes (for an unknown reason) it fills in the background of the image with a random color from the color table it uses.( In this case, bright green... it should all be transparent!) Since I cannot figure out why, I need to somehow use IM to find what color this is, and make it transparent. Example image is below. The very bottom left corner is always going to be out of range of the radar, so if a script can somehow determine whatever color that pixel is (I pointed out the location that is always best to use), that would work just great.
If I can get this figured out - I can make a simple script to make it transparent. I am just lost as to how to determine what color it is.
Re: Getting the color of a pixel
Posted: 2018-09-19T00:49:00-07:00
by dholcomb9711
Disregard this reply, I had trouble with my internet connection. I edited my question above. I still need help.
Re: Getting the color of a pixel
Posted: 2018-09-19T03:00:06-07:00
by snibgo
For integer formats such as JPEG and PNG, "hex:" is useful, eg:
Code: Select all
magick in.png -format %[hex:p{0,99}] info:
Instead of 99, use the height minus one.
This gives the colour in hex format. If the image has transparency, the fourth quarter is the alpha channel, which you need to test whether the pixel is already transparent.
Re: Getting the color of a pixel
Posted: 2018-09-19T04:32:47-07:00
by dholcomb9711
Okay, forgive my lack of skills here as I am trying to follow along, it's a new command for me.
My filename is "KEWX_L2_VEL_colorized.gif" and the height is 3770px. So my command was :
Code: Select all
magick KEWX_L2_VEL_colorized.gif -format p{0,3769}] info:
It returned :
I was expecting a hex value. Can you explain if I am missing something?
Also, if I do get the hex value... how can I then launch the command to make it transparent? My 2nd command would be :
Code: Select all
magick KEWX_L2_VEL_colorized.gif -transparent #5BD13D KEWX_L2_VEL_colorized.gif
That part is easy, but how do I get it TO that point? (I am making this automated in the windows command prompt). I am trying to follow along as I know you know what you are talking about! You are very helpful here. I just struggle sometimes with my commands.
Re: Getting the color of a pixel
Posted: 2018-09-19T04:39:28-07:00
by snibgo
Compare your command to mine. Mine included characters in the format that yours doesn't.
You haven't said what shell you use (bash, Windows CMD, or whatever). When your magick command works, use shell facilities to put the result in a variable, then you can use that variable in your 2nd command.
Re: Getting the color of a pixel
Posted: 2018-09-19T04:51:54-07:00
by dholcomb9711
Ah, I had edited my post to let you know I am using the windows command line, but I think it was slow to update the edit. I am still struggling to get this working. What is the name of the variable in the command you showed me? Am I thinking too hard?
Re: Getting the color of a pixel
Posted: 2018-09-19T05:57:34-07:00
by snibgo
Here's the command I showed you:
Code: Select all
magick in.png -format %[hex:p{0,99}] info:
You should put in your own filename, and height minus 1:
Code: Select all
magick KEWX_L2_VEL_colorized.gif -format %[hex:p{0,3769}] info:
Does that work okay?
Re: Getting the color of a pixel
Posted: 2018-09-19T14:22:00-07:00
by dholcomb9711
It does not work. My command is exactly as follows (I used notepad to save it as a batch file and ran it):
Code: Select all
magick KEWX_L2_VEL_colorized.gif -format %[hex:p{0,3769}] info:
When I launch it in the windows 10 command prompt, the command seems to be different than what I actually told it to do. (screenshot) The '%' an the first '[' are gone.Not sure what is going on here.
Edit : In case it helps - Version: ImageMagick 7.0.7-35 Q16 x64 2018-05-21
Re: Getting the color of a pixel
Posted: 2018-09-19T14:57:17-07:00
by snibgo
dholcomb9711 wrote:(I used notepad to save it as a batch file and ran it):
In a BAT file, you need to double the %.
Re: Getting the color of a pixel
Posted: 2018-09-19T15:05:35-07:00
by dholcomb9711
That worked! Output is "4BCB2AFF".
How do I get this into the next command? My command should be -
Code: Select all
magick KEWX_L2_VEL_colorized.gif -transparent #4BCB2AFF KEWX_L2_VEL_colorized.gif
This works when I manually type it, but I should be able to inject the variable in there to automate it somehow. I can't thank you enough for your help. I am learning in this process.
Re: Getting the color of a pixel
Posted: 2018-09-20T03:21:23-07:00
by snibgo
Like this, Windows BAT syntax:
Code: Select all
for /F "usebackq" %%L in (`magick ^
KEWX_L2_VEL_colorized.gif ^
-format "mycol=%%[hex:p{0,3769}]\n" ^
info:`) do set %%L
magick KEWX_L2_VEL_colorized.gif -transparent #%mycol% KEWX_L2_VEL_colorized.gif
Re: Getting the color of a pixel
Posted: 2018-09-20T14:25:20-07:00
by GeeMack
dholcomb9711 wrote: ↑2018-09-19T15:05:35-07:00Code: Select all
magick KEWX_L2_VEL_colorized.gif -transparent #4BCB2AFF KEWX_L2_VEL_colorized.gif
This works when I manually type it, but I should be able to inject the variable in there to automate it somehow.
Since you're using IM7, in many cases you can do this sort of thing as a single command by reading the pixel's color and applying it directly in the operation where you need it. This should give you a result like your example...
Code: Select all
magick KEWX_L2_VEL_colorized.gif -transparent %[pixel:p{0,3769}] KEWX_L2_VEL_colorized.gif
Again, if you use it in a BAT script you need to double the percent "%" signs.
Re: Getting the color of a pixel
Posted: 2018-09-20T15:29:52-07:00
by snibgo
GeeMack wrote:-transparent %[pixel:p{0,3769}]
Ah, I didn't know we could do that. Very useful. Thanks, GeeMack.
Re: Getting the color of a pixel
Posted: 2018-09-20T15:54:40-07:00
by fmw42
Nice. Note that you can also use hex:p{0,3769}], but you have to preface it with #. So it would be in Unix syntax
magick KEWX_L2_VEL_colorized.gif -transparent "#%[hex:p{0,3769}]" KEWX_L2_VEL_colorized.gif
or I presume in Windows, this will work without the quotes, but someone should check that out.
magick KEWX_L2_VEL_colorized.gif -transparent #%[hex:p{0,3769}] KEWX_L2_VEL_colorized.gif
Re: Getting the color of a pixel
Posted: 2018-09-20T16:50:23-07:00
by GeeMack
fmw42 wrote: ↑2018-09-20T15:54:40-07:00or I presume in Windows, this will work without the quotes, but someone should check that out.
magick KEWX_L2_VEL_colorized.gif -transparent
#%[
hex:p{0,3769}] KEWX_L2_VEL_colorized.gif
That works in Windows with or without the quotes. Either way requires doubling the percent sign if used in a BAT script, of course.