Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Imagick. Changing a color to be transparent

I would like to change the black border of this image : http://dev.loungeup.net/im/ to transparent with Imagick in PHP.

The result image should have the gray border visible, the image inside the gray border visible, and everything outside the gray border (today in black) should be transparent and let the underlying content (in HTML page for example) visible.

I have been through the documentation several times and tried several solutions but did not figure out how to achieve it.

Any help is appreciated.

Thanks

like image 443
Lionel Tressens Avatar asked Dec 10 '22 02:12

Lionel Tressens


1 Answers

Here's one method you could use which is similar to Photoshop's magic wand tool:

convert original.jpg -alpha set -channel alpha -fuzz 18% \
-fill none -floodfill +0+0 black transparent-border.png

Here are the commands broken down:

convert original.jpg: start Imagemagick with the original image

-alpha set: activate the alpha channel

-channel alpha: have subsequent operators act on alpha channel

-fuzz 18%: see -floodfill...

-fill none: see -floodfill...

-floodfill +0+0 black: start from the upper left corner (+0+0) and find neighboring pixels within -fuzz color distance of black and replace it will -fill

transparent-border.png: the output image

Here's the result: Imagemagick Method 1

Unfortunately, as you can see, this method still leaves some dark pixels with the image you provided because the border is not pure black and blends a bit with the inner gray border, and the image itself is quite small.

You will get much better results if you have a better-quality source image, or a larger one which you could then reduce in size after modifying.

If you're stuck with these small images (or if you just want to use another method), I would recommend going a different route where you create your own mask shape that is just smaller than the shape of the original photo, then add back your own gray border. I put together an example of this process below.

A possible command for this method would be:

convert original.jpg mask.png -compose CopyOpacity -composite \
-compose src-over new-border.png -composite clean-result.png

... broken down ...

convert original.jpg mask.png: start Imagemagick with original image and bring in mask.png as the second layer (mask.png is a white rounded-rectangle shape of the photo on a black background, but the shape is slightly smaller than that of the original - the result will remove the original's gray and black border).

-compose CopyOpacity -composite: use mask.png to "knock out" shape from original.png

-compose src-over: reset composite method to a simple overlay

new-border.png -composite: overlay a gray border (png is a 3 px wide border, 1px on each side of mask edge on a transparent background)

clean-result.png: the output image

I created mask.png and new-border.png in Photoshop. You could use Imagemagick's vector tools and do this all in one command using just original.jpg, but it wouldn't be easy.

The result of the above: Imagemagick Method 2

On a final note, I'm not sure whether you're using PHP's Imagick or the Imagemagick from the command line. Several years ago I tried using Imagick but quickly became frustrated from the lack of documentation compared to the command line (perhaps that's changed, though). Instead, I execute the commands from PHP (e.g., from exec() or passthru()). Some or many others will probably tell you that you should never execute shell commands from PHP, but as long as you carefully escape any arguments I have yet to see a convincing argument against doing so. Then you'll have the entire Imagemagick documentation at your disposal (http://www.imagemagick.org/Usage/).

Cheers.

like image 136
Synexis Avatar answered Dec 30 '22 17:12

Synexis