Getting the color of a pixel
-
- Posts: 16
- Joined: 2018-06-08T03:45:59-07:00
- Authentication code: 1152
Getting the color of a pixel
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.
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.
Last edited by dholcomb9711 on 2018-09-19T00:55:13-07:00, edited 1 time in total.
-
- Posts: 16
- Joined: 2018-06-08T03:45:59-07:00
- Authentication code: 1152
Re: Getting the color of a pixel
Disregard this reply, I had trouble with my internet connection. I edited my question above. I still need help.
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Getting the color of a pixel
For integer formats such as JPEG and PNG, "hex:" is useful, eg:
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.
Code: Select all
magick in.png -format %[hex:p{0,99}] info:
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.
snibgo's IM pages: im.snibgo.com
-
- Posts: 16
- Joined: 2018-06-08T03:45:59-07:00
- Authentication code: 1152
Re: Getting the color of a pixel
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 :
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 :
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.
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:
Code: Select all
p{0,3769}]
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
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Getting the color of a pixel
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.
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.
snibgo's IM pages: im.snibgo.com
-
- Posts: 16
- Joined: 2018-06-08T03:45:59-07:00
- Authentication code: 1152
Re: Getting the color of a pixel
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?
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Getting the color of a pixel
Here's the command I showed you:
You should put in your own filename, and height minus 1:
Does that work okay?
Code: Select all
magick in.png -format %[hex:p{0,99}] info:
Code: Select all
magick KEWX_L2_VEL_colorized.gif -format %[hex:p{0,3769}] info:
snibgo's IM pages: im.snibgo.com
-
- Posts: 16
- Joined: 2018-06-08T03:45:59-07:00
- Authentication code: 1152
Re: Getting the color of a pixel
It does not work. My command is exactly as follows (I used notepad to save it as a batch file and ran it):
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
Code: Select all
magick KEWX_L2_VEL_colorized.gif -format %[hex:p{0,3769}] info:
Edit : In case it helps - Version: ImageMagick 7.0.7-35 Q16 x64 2018-05-21
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Getting the color of a pixel
In a BAT file, you need to double the %.dholcomb9711 wrote:(I used notepad to save it as a batch file and ran it):
snibgo's IM pages: im.snibgo.com
-
- Posts: 16
- Joined: 2018-06-08T03:45:59-07:00
- Authentication code: 1152
Re: Getting the color of a pixel
That worked! Output is "4BCB2AFF".
How do I get this into the next command? My command should be -
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.
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
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Getting the color of a pixel
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
snibgo's IM pages: im.snibgo.com
- GeeMack
- Posts: 718
- Joined: 2015-12-01T22:09:46-07:00
- Authentication code: 1151
- Location: Central Illinois, USA
Re: Getting the color of a pixel
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...dholcomb9711 wrote: ↑2018-09-19T15:05:35-07:00This works when I manually type it, but I should be able to inject the variable in there to automate it somehow.Code: Select all
magick KEWX_L2_VEL_colorized.gif -transparent #4BCB2AFF KEWX_L2_VEL_colorized.gif
Code: Select all
magick KEWX_L2_VEL_colorized.gif -transparent %[pixel:p{0,3769}] KEWX_L2_VEL_colorized.gif
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Getting the color of a pixel
Ah, I didn't know we could do that. Very useful. Thanks, GeeMack.GeeMack wrote:-transparent %[pixel:p{0,3769}]
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: Getting the color of a pixel
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
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
- GeeMack
- Posts: 718
- Joined: 2015-12-01T22:09:46-07:00
- Authentication code: 1151
- Location: Central Illinois, USA
Re: Getting the color of a pixel
That works in Windows with or without the quotes. Either way requires doubling the percent sign if used in a BAT script, of course.