Page 1 of 1

[Solved] How to remove all color except 3, and resize canvas

Posted: 2011-05-15T10:18:52-07:00
by vldvrfc
Hi all.

input.png:
Image

I use:

Code: Select all

convert input.png ( -clone 0 -fill white +opaque #339966 -transparent white ) ( -clone 0 -fill white +opaque #FFCC00 -transparent white ) ( -clone 0 -fill white +opaque #CCFFCC -transparent white ) -delete 0 -background white -flatten output.png
output.png:
Image

I want to separate them, like this:
( Note: I don't know the color order, I just know these colors, and the result must be the same order. Sorry I didn't told this condition before.)
Image

I had tried ( -clone 0 -fill white +opaque #339966 -transparent white -background transparent -extent 200% -gravity center ), but failed.

Re: How to remove all color except 3 color, and resize Canva

Posted: 2011-05-15T10:37:10-07:00
by fmw42
Make a mask for each color you want to save and isolate that color as a new image with a transparent background. Then trim each image. Then append or flatten over a white background separated as needed using white spacer images for append (and remove the transparency) or use -page to set the position with -flatten

Re: How to remove all color except 3 color, and resize Canva

Posted: 2011-05-15T11:23:14-07:00
by Bonzo
This is something I was working on a few years ago:

Code: Select all

<?php 
// Colours to replace 
$mask_array = array('#5c89cc', '#bb8600', '#2c3516'); 
// Allowance on colour to replace 
$fuzz_array = array('28', '20', '21'); 
// Colour to replace with 
$replace_array = array('blue', 'yellow', 'green'); 

// Get the size of the image and create a tempory background  
// This can be useful to fill any transparent areas left. 
$size = getimagesize('input.jpg'); 
exec("convert -size {$size[0]}x{$size[1]} xc:blue background.miff"); 

// Find the total number of masks to create 
$number = count( $mask_array ); 
// Remove one as the array stats at 0 
$number = $number -1 ; 

// Setup the initial value for $cmd 
$cmd = ''; 

// Loop through and create the masks 
// The masks are going to overwrite the previously created mask on each loop 
// The mask will be added to the image on each loop before being overwitten 
// I have used the dedicated ImageMagick image formt 
for ( $i=0; $i<=$number; $i++ ) { 
$cmd = "input.jpg -matte ( +clone -fuzz {$fuzz_array[$i]}% -transparent {$mask_array[$i]} )". 
" -compose DstOut -composite -fill {$replace_array[$i]} -fuzz 60% -opaque {$mask_array[$i]} "; 

exec("convert $cmd mask.miff"); 

if ( $i == '0' ) { $cmd = " background.miff mask.miff -composite "; } 
else { $cmd = "output.miff mask.miff -composite "; } 
exec("convert $cmd output.miff"); 

} 
// Create the final image 
exec("convert output.miff output.png"); 

// Delete the tempory images 
$delete_array = array ( 'background.miff', 'mask.miff', 'output.miff' ); 
foreach ( $delete_array as $value ) 
{ 
unlink( $value ); 
} 
Image

Re: How to remove all color except 3 color, and resize Canva

Posted: 2011-05-15T14:27:14-07:00
by fmw42
try this on Linux or Mac. if on Windows, see http://www.imagemagick.org/Usage/windows/


hh=`convert Bkwme.png -ping -format "%h" info:`
ww=`convert Bkwme.png -ping -format "%w" info:`
ww1=`convert xc: -format "%[fx:$ww/2 - 2*$hh]" info:`
ww2=`convert xc: -format "%[fx:$ww/2 - $hh/2]" info:`
ww3=`convert xc: -format "%[fx:$ww/2 + $hh]" info:`

convert -respect-parenthesis Bkwme.png -size ${ww}x${hh} xc:white \
\( -page +${ww1}+0 -clone 0 -fuzz 1% +transparent "#339966" -trim +repage \
-background none -gravity center -extent x${hh} \) \
\( -page +${ww2}+0 -clone 0 -fuzz 1% +transparent "#FFCC00" -trim +repage \
-background none -gravity center -extent x${hh} \) \
\( -page +${ww3}+0 -clone 0 -fuzz 1% +transparent "#CCFFCC" -trim +repage \
-background none -gravity center -extent x${hh} \) \
-delete 0 -flatten Bkwme_separate.png

Image

see
http://www.imagemagick.org/Usage/layers/#flatten
http://www.imagemagick.org/Usage/crop/#trim
http://www.imagemagick.org/Usage/crop/#extent

Note: +transparent makes everything transparent except the specified color

Re: How to remove all color except 3 color, and resize Canva

Posted: 2011-05-15T20:46:34-07:00
by vldvrfc
Thanks a lot.

Sorry I forgot to tell the condition:
I don't know the color order, I just know these colors, and the result must be the same order.

but the answers is still usefull for me.

Re: How to remove all color except 3 color, and resize Canva

Posted: 2011-05-15T22:30:47-07:00
by HugoRune
Perhaps like this:
- split into 3 images
- trim right side of each image
- extend right side of each image by x%
- combine images, aligned to the left

The more on the right the blob appears, the less will be trimmed, the more the image will be extended.
So the original order is preserved, and the blobs are shifted apart.

(command line for windows)

Code: Select all

convert -respect-parenthesis Bkwme.png -write MPR:source -delete 0 ^
(  MPR:source -fuzz 1% +transparent "#FFCC00" ^
   -background black -gravity west -splice 1x -trim +repage -chop 1x0 ^
   -background none  -gravity east -extent 300%x100% ) ^
(  MPR:source -fuzz 1% +transparent "#CCFFCC" ^
   -background black -gravity west -splice 1x -trim +repage -chop 1x0 ^
   -background none  -gravity east -extent 300%x100% ) ^
(  MPR:source -fuzz 1% +transparent "#339966" ^
   -background black -gravity west -splice 1x -trim +repage -chop 1x0 ^
   -background none  -gravity east -extent 300%x100% ) ^
-background white -gravity west -layers merge -trim +repage Bkwme_separate.png
Image

if you need more fine grained control, you could read the exact position of each blob with -trim info:-
http://www.imagemagick.org/Usage/crop/#trim

(EDIT: fixed a mistake, should work now)

Re: How to remove all color except 3 color, and resize Canva

Posted: 2011-05-16T14:04:24-07:00
by fmw42
This will use the trim page x-offsets to sort the images so that it keeps the left to right order of the colors.


hh=`convert Bkwme.png -ping -format "%h" info:`
ww=`convert Bkwme.png -ping -format "%w" info:`
spacer=`convert xc: -format "%[fx:($ww-3*$hh)/4]" info:`

convert Bkwme.png \
\( -clone 0 -fuzz 1% +transparent "#339966" -trim -write Bkwme_1.png \) \
\( -clone 0 -fuzz 1% +transparent "#FFCC00" -trim -write Bkwme_2.png \) \
\( -clone 0 -fuzz 1% +transparent "#CCFFCC" -trim -write Bkwme_3.png \) \
-delete 0 null:

xx1=`convert Bkwme_1.png -ping -format "%[fx:page.x]" info:`
xx2=`convert Bkwme_2.png -ping -format "%[fx:page.x]" info:`
xx3=`convert Bkwme_3.png -ping -format "%[fx:page.x]" info:`
list1="Bkwme_1.png $xx1
Bkwme_2.png $xx2
Bkwme_3.png $xx3"
list2=`echo "$list1" | sort -k 2`
img1=`echo $list2 | cut -d\ -f1`
img2=`echo $list2 | cut -d\ -f3`
img3=`echo $list2 | cut -d\ -f5`

convert -size ${ww}x${hh} xc:white \
$img1 +repage -background none -gravity center -extent x$hh -gravity west -geometry +$spacer+0 -composite \
$img2 +repage -background none -gravity center -extent x$hh -gravity center -composite \
$img3 +repage -background none -gravity center -extent x$hh -gravity east -geometry +$spacer+0 -composite \
Bkwme_composite.png

rm -f $img1 $img2 $img3

Image

Re: How to remove all color except 3 color, and resize Canva

Posted: 2011-05-16T21:25:11-07:00
by vldvrfc
Thank you for your help.