Trying to replace certain colors

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
leeh

Trying to replace certain colors

Post by leeh »

Hi i was trying to swap out colors from a template image with other colors, i decided to use the MagickPaintFloodfillImage function to do this. I am not sure if this was a good choice; I am running that function through PHP and was wondering if my code below is correct.

Code: Select all

<?php

$r = NewMagickWand();

MagickReadImage($r, 'redblack.JPG');

$color = NewPixelWand();
PixelSetColor($color, "blue");
$border = NewPixelWand();
PixelSetColor($border, "black");

echo "working checkpoint<br>";

if ( MagickPaintFloodfillImage( $r, $color, 0, $border, 50, 50) != 0 )
{
	echo "success";
	// MagickEchoImageBlob( $r );
}
else
{ 
	 echo MagickGetExceptionString($r);
}
?>
I can get to the "working checkpoint" string but nothing happens afterwards, so this makes me wonder if i'm using the function correctly. Example codes online work with my image but this is my first personal attempt with imagemagick that didn't turn out right.

Also i don't seem to get any error messages, this is really limiting my debugging.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Trying to replace certain colors

Post by Bonzo »

If using php I would try this ( from an old post http://redux.imagemagick.org/discourse- ... ange+color )

Code: Select all

<?php
exec("/usr/local/bin/convert original.jpg -fill rgb\(255,0,0\) -opaque rgb\(170,170,170\) output.jpg");  
?>
-fill is the new colour and -opaque is the colour to change. You can use colour names rgb, hex etc. Adding a -fuzz will give you a range of shades around the colour you want to change.
leeh

Re: Trying to replace certain colors

Post by leeh »

Thanks, that code worked... but but made me realize the folder had to have a chmod of 777... which isn't what i was looking forwards to.

Thanks though!

also this works:

Code: Select all

exec("convert black.JPG -fuzz 10 -fill '#345678' -opaque black new_black.jpg");
for those like me who like to deal with hex
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Trying to replace certain colors

Post by anthony »

You don't have to write out the image. You can output it to stand out.

<?PHP
header( 'Content-Type: image/jpeg' );
exec("convert original.jpg -fill rgb\(255,0,0\) -opaque rgb\(170,170,170\) JPG:-");
?>
see IM Examples Feedback page
http://www.imagemagick.org/Usage/feedback.html#php
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply