Page 1 of 1

Color half image

Posted: 2009-11-03T04:32:14-07:00
by CrispyCookie
I would try this script to color the left half of my images green:

Code: Select all

convert leftgreen: image.png
convert leftgreen: -crop 32x32+0+0 -background green image.png
I haven't tried it yet, but there's a problem, since my images aren't always 64x32.

If you don't get what I want, I want the left half colored green (#00FF00) and retain the right half, at images which are either 64x32, 64x64, 128x32 or 128x64.

Is there a universal solution like

Code: Select all

convert leftgreen: image.png
convert leftgreen: -crop [fx:w/2]x[fx:h/2]+0+0 -background green image.png
?

Thanks in advance.

Re: Color half image

Posted: 2009-11-03T11:24:27-07:00
by fmw42
this might be a little slow, but is the simplest

convert image.png -fx "i<=w/2?green1:u" image_leftgreen.png

see fx at http://www.imagemagick.org/script/fx.php


Here is the more complicated way:

convert image.png \
\( +clone -set option:distort:viewport "%[fx:w/2]"x"%[fx:h]"+0+0 \ -distort SRT "1 0" \
-threshold -1 -fill green1 -opaque white \) \
-composite image.png

Re: Color half image

Posted: 2009-11-08T18:06:38-07:00
by anthony
Why not divide the image in two, color the left half and merge together again.

Code: Select all

   convert  image.png  -crop 50x100% \
               \( -clone 0 -fill green -colorize 100% \) \
               -delete 0 -background none -flatten  result.png
Or instead of layer merging, try append instead.

Code: Select all

   convert  image.png  -crop 50x100% +repage \
               \( -clone 0 -fill green -colorize 100% \) \
               -swap 0 +delete  +append  result.png
See IM examples Image Sequence Operators and tile cropping

Either method should work fine for any single image. Though their will be differences for images which already involve 'layer' or 'virtual canvas' handling. I don't think that would be the case however.