Nick
Nick

Reputation: 2805

Imagemagick - Select area to replace pixel colour

I am trying to replace the colour of some pixels in an image using imagemagick, BUT not in the whole image just within a part of the image.

The command I am using now is:

convert original_image.jpg -fuzz 5% -fill '#f2f2f2' -opaque white image_without_colour.jpg

This works but it replaces #f2f2f2 everywhere rather than just where i want i want.

I would ideally do something like:

convert original_image.jpg -fuzz 5% -fill '#f2f2f2' -area '100,100,50,50' -opaque white image_without_colour.jpg

Upvotes: 1

Views: 1775

Answers (2)

Glenn Randers-Pehrson
Glenn Randers-Pehrson

Reputation: 12465

ImageMagick's "-region" option is equivalent to your suggested "-area" option:

convert original_image.jpg -fuzz 5% -fill '#f2f2f2' -region 100x100+50+50 \
        -opaque white image_without_colour.jpg

changes white pixels in the specified region to gray.

Upvotes: 1

Mark Setchell
Mark Setchell

Reputation: 207670

I am thinking about a more elegant solution, but I think this does what you want:

convert x.jpg -crop 100x100+50+50 y.jpg                     # Extract region to work on into y.jpg
convert y.jpg -fuzz 5% -fill '#f2f2f2' -opaque white z.jpg  # Do your stuff and save as z.jpg
convert x.jpg z.jpg -geometry +50+50 -composite out.jpg     # Whack it back whence it came!

Output file isout.jpg

I think this is a more elegant solution:

convert x.png \( +clone -crop 100x100+50+50 -fuzz 5% -fill '#f2f2f2' -opaque white \) -flatten out.jpg

Upvotes: 1

Related Questions