Back to the snippets index

Warhol effect ( Anthony 9/8/2008 )

This method should work with most versions; it could be altered to use the -layers option which started around 6.3.5
You can add as many colours to the array as you would like as the code will run them all. You may lose some colours depending on the order you define them.
This process is NOT very easy to automate as it needs a lot of user input.

IM code:

// 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 -;

// 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 to go with Warhol effect example code
Batch file - windows server
Check size before resizing
Dividing an image
Fonts Installed view
Get image data into a variable 1
Get image data into a variable 2
Gravity option
New image size in variable
Row of images
Setup Information
Use an array
Warhol effect