简体   繁体   English

使用Image Magick获取图像形状边缘

[英]Get image shape edges with Image Magick

I am new to ImageMagick and Imagick library in php, and I want to know how can I make everything transparent but leave only the edges of the shape in a png image. 我是php中ImageMagick和Imagick库的新手,我想知道如何使所有内容透明,而在png图像中仅保留形状的边缘。

For example I have this picture: 例如,我有这张照片: 在此处输入图片说明

And I want to keep only a thin line formed by the edges of the eagle, and fill the inside of the eagle with transparent color. 我只想保留由鹰的边缘形成的细线,并用透明的颜色填充鹰的内部。

Or as an alternative how can I change the black eagle color into a brown rgba color with opacity 0.4 using Image Magick library with php and GD library? 或者作为替代方案,如何使用Image Magick库以及php和GD库将黑鹰颜色更改为不透明度为0.4的棕色rgba颜色?

Something like this to detect edges: 像这样的东西来检测边缘:

convert eagle.png --edge 3 eagle-edges.png

Within PHP you can use bool Imagick::edgeImage(float $radius) . 在PHP中,您可以使用bool Imagick::edgeImage(float $radius)

I managed to get the result with these commands though I haven't tested yet on other images, only on the one with the eagle. 尽管我还没有在其他图像上进行测试,但仅在使用老鹰的图像上进行测试,但是我设法通过这些命令获得了结果。

exec('convert eagle.png -charcoal 1 neweagle.png');
exec('convert neweagle.png -transparent white neweagle.png'); 

I am open to other better solutions!! 我愿意接受其他更好的解决方案!!

Also would be great if some knows how to do these commands from the php library without the exec. 如果有人知道如何在没有exec的情况下从php库执行这些命令,那也将很棒。

Option 1 选项1

If you want the outline of the eagle black and everything else transparent, you can use this: 如果您希望鹰黑色的轮廓和其他所有物体都透明,则可以使用以下方法:

convert eagle.png -channel RGBA -separate -delete 0-2 -edge 1 -threshold 1 -transparent black +channel -negate result.png

在此处输入图片说明

That separates the image into RGBA channels and discards all except the alpha channel. 它将图像分成RGBA通道,并丢弃除alpha通道以外的所有通道。 It then finds the edges of the alpha channel (which show up as white on black) and then makes black pixels transparent and inverts the white ones to black. 然后,它找到alpha通道的边缘(在黑色上显示为白色),然后使黑色像素透明,并将白色像素反转为黑色。

My PHP implementation is as follows: 我的PHP实现如下:

<?php
// convert eagle.png -channel RGBA -separate -delete 0-2 -edge 1 -threshold 1 -transparent black +channel -negate result.png
   $image=new Imagick('eagle.png');
   $image->separateimagechannel(Imagick::CHANNEL_OPACITY);
   $image->edgeImage(1);
   $image->thresholdImage(1);
   $image->transparentPaintImage("black",0.0,10,false);
   $image->negateImage(false);
   $image->writeimage('result.png');
?>

Option 2 选项2

If you want to make the black body of the eagle into 40% opaque dark brown, you can do this: 如果要将老鹰的黑体变成40%不透明的深棕色,可以执行以下操作:

convert eagle.png -channel RGBA -fill "rgba(101,67,33,0.4)" -fuzz 10% -opaque black result.png

在此处输入图片说明

My PHP implementation is as follows: 我的PHP实现如下:

<?php
// convert eagle.png -channel RGBA -fill "rgba(101,67,33,0.5)" -fuzz 10% -opaque black answer2.png
   $image=new Imagick('eagle.png');
   $image->opaquePaintImage("black","rgba(101,67,33,0.4)",10,false,imagick::CHANNEL_ALL);
   $image->writeimage('result2.png');
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM