Page 1 of 1
Getting X and Y coordinates based on color change
Posted: 2008-08-28T09:24:41-07:00
by Zarag0z
First of all, congrats for your great tools, they are amazing.
Now here's my question:
I currently use "convert balloon.gif -fill white -opaque blue balloon_white.gif" to change colors in my images. I was now wondering if it was possible to get either:
- The X and Y coordinates of the pixels that are being changed (from blue to white in this example)
or
- the Polygon coordinates of the area changed (from blue to white in this example).
Is this possible?
Thanks!
Re: Getting X and Y coordinates based on color change
Posted: 2008-08-28T11:17:57-07:00
by fmw42
You can get the individual pixels changed using a different method before changing them. See my tidbit on this at
http://www.fmwconcepts.com/imagemagick/ ... r_location
Here is an example using the logo: image scaled to 64x48 pixels so that the listing is not too big.
str=`convert logo: -scale 64x48 -fx "u==white?debug(u):1; u" null: 2>&1`
echo "$str" | sed -n 's/^.*\[\([0-9]*\),\([0-9]*\)\]\.red.*$/\2,\1/p'
You can also do it as one command line:
echo "$(convert logo: -scale 64x48 -fx "u==white?debug(u):1; u" null: 2>&1)" | \
sed -n 's/^.*\[\([0-9]*\),\([0-9]*\)\]\.red.*$/\2,\1/p'
This lists the pixels and changes them in one command:
echo "$(convert \( logo: -scale 64x48 \) \
\( +clone -fill red -opaque white -write logo_white2red.gif +delete \) \
-fx "u==white?debug(u):1; u" null: 2>&1)" | \
sed -n 's/^.*\[\([0-9]*\),\([0-9]*\)\]\.red.*$/\2,\1/p'
Note in the above, I am changing white to red (not blue to white) so that you can see what is going on in the logo image.
Re: Getting X and Y coordinates based on color change
Posted: 2008-08-28T11:39:19-07:00
by Zarag0z
Thx for the answer, but I got to your site and I dont really understand the examples that you have.
Is it possible to show me a simple example where I would get the locations of lets say all the white (255,255,255) pixels contained in one image?
Thanks
Re: Getting X and Y coordinates based on color change
Posted: 2008-08-28T11:53:51-07:00
by fmw42
As in the above, here is one example which changes all white pixels to red and lists the coordinates.
convert logo: -scale 64x48 logo_small.gif
echo "$(convert logo_small.gif \
\( +clone -fill red -opaque white -write logo_white2red.gif +delete \) \
-fx "u==white?debug(u):1; u" null: 2>&1)" | \
sed -n 's/^.*\[\([0-9]*\),\([0-9]*\)\]\.red.*$/\2,\1/p'
Here is just the part that lists the pixel coord for all white pixels:
echo "$(convert logo_small.gif -fx "u==white?debug(u):1; u" null: 2>&1)" | \
sed -n 's/^.*\[\([0-9]*\),\([0-9]*\)\]\.red.*$/\2,\1/p'
Note the sed is Unix to get just the coordinates and reverse y,x (from debug) to x,y
If you leave off the sed as in the following, you will still get a listing, but it will have color values and image name also for each coord.
echo "$(convert logo_small.gif -fx "u==white?debug(u):1; u" null: 2>&1)"
or
convert logo_small.gif -fx "u==white?debug(u):1; u" null: 2>&1
on PC, I am not sure if 2>&1 is the same. This lets the stderr go to stdout.
Re: Getting X and Y coordinates based on color change
Posted: 2008-09-01T22:00:25-07:00
by anthony
You can get a list of the pixels by doing a search ('grep') on a M Enumerated Pixel Format or 'txt:' image.
For example using balloon and searching for exact 'blue' matches...
Code: Select all
convert balloon.gif txt: | grep 'blue'
the first two numbers is the pixel location!!!
However a better solution is NOT to rely on the color name 'blue'
but the numbers in the parenthesis. Also adding a -depth
setting lets you ensure which bit-depth those numbers are in.
Code: Select all
convert balloon.gif -depth 16 txt: | grep '( *0, *0, *65535)'
If you also want to find the pixels that are 'close' to the color,
use -fuzz and -opaque to make them that color.
Code: Select all
convert balloon.gif -fuzz 10% -fill blue -opaque blue -depth 16 txt: | grep '( *0, *0, *65535)'
For info om the IM Enumerated Pixel Format or 'txt:' format
See
http://www.imagemagick.org/Usage/files/#txt
for information on color matching and fuzz look where you got your original example
http://www.imagemagick.org/Usage/color/#replace