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!
Getting X and Y coordinates based on color change
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Getting X and Y coordinates based on color change
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.
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.
Last edited by fmw42 on 2008-08-28T11:52:35-07:00, edited 2 times in total.
Re: Getting X and Y coordinates based on color change
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
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
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Getting X and Y coordinates based on color change
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.
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.
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: Getting X and Y coordinates based on color change
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...
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.
If you also want to find the pixels that are 'close' to the color,
use -fuzz and -opaque to make them that color.
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
For example using balloon and searching for exact 'blue' matches...
Code: Select all
convert balloon.gif txt: | grep 'blue'
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)'
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)'
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
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/