开发者

How can I detect borders of a piece of paper inside of an image and crop it using ImageMagick?

开发者 https://www.devze.com 2023-03-28 16:45 出处:网络
I\'m trying to crop paper documents from photographs.For example, someone takes a picture of a document and sends it to the server and it will get edited 开发者_如何学Goto look like a scanned document

I'm trying to crop paper documents from photographs. For example, someone takes a picture of a document and sends it to the server and it will get edited 开发者_如何学Goto look like a scanned document. How can I detect the border of the document and crop it using ImageMagick?

Thanks


You want to use the convert command with the -trim option to crop out the solid color borders, and since the images are scanned, the -fuzz option to make sure trim completely crops the border even if the color isn't perfectly solid.

So something like this:

convert input.jpg -fuzz 2% -trim output.jpg

Translating that into PHP code, you'd end up with this:

$image = new Imagick('input.jpg');
$image->trimImage(2); // Trim the image with a 2% fuzz
$image->writeImage('output.jpg');
0

精彩评论

暂无评论...
验证码 换一张
取 消