Color half image

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?".
Post Reply
CrispyCookie

Color half image

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Color half image

Post 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
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Color half image

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply