Getting the color of a pixel

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
dholcomb9711
Posts: 16
Joined: 2018-06-08T03:45:59-07:00
Authentication code: 1152

Getting the color of a pixel

Post 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.

Image
Last edited by dholcomb9711 on 2018-09-19T00:55:13-07:00, edited 1 time in total.
dholcomb9711
Posts: 16
Joined: 2018-06-08T03:45:59-07:00
Authentication code: 1152

Re: Getting the color of a pixel

Post by dholcomb9711 »

Disregard this reply, I had trouble with my internet connection. I edited my question above. I still need help.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Getting the color of a pixel

Post 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.
snibgo's IM pages: im.snibgo.com
dholcomb9711
Posts: 16
Joined: 2018-06-08T03:45:59-07:00
Authentication code: 1152

Re: Getting the color of a pixel

Post 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 :

Code: Select all

p{0,3769}]
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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Getting the color of a pixel

Post 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.
snibgo's IM pages: im.snibgo.com
dholcomb9711
Posts: 16
Joined: 2018-06-08T03:45:59-07:00
Authentication code: 1152

Re: Getting the color of a pixel

Post 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?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Getting the color of a pixel

Post 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?
snibgo's IM pages: im.snibgo.com
dholcomb9711
Posts: 16
Joined: 2018-06-08T03:45:59-07:00
Authentication code: 1152

Re: Getting the color of a pixel

Post 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.

Image


Edit : In case it helps - Version: ImageMagick 7.0.7-35 Q16 x64 2018-05-21
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Getting the color of a pixel

Post 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 %.
snibgo's IM pages: im.snibgo.com
dholcomb9711
Posts: 16
Joined: 2018-06-08T03:45:59-07:00
Authentication code: 1152

Re: Getting the color of a pixel

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Getting the color of a pixel

Post 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
snibgo's IM pages: im.snibgo.com
User avatar
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

Post by GeeMack »

dholcomb9711 wrote: 2018-09-19T15:05:35-07:00

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.
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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Getting the color of a pixel

Post by snibgo »

GeeMack wrote:-transparent %[pixel:p{0,3769}]
Ah, I didn't know we could do that. Very useful. Thanks, GeeMack.
snibgo's IM pages: im.snibgo.com
User avatar
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

Post 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
User avatar
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

Post 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.
Post Reply