skybluesofa/image-barbershop

智能、自动图像裁剪

1.3.0 2020-03-10 00:26 UTC

This package is auto-updated.

Last update: 2024-09-10 10:00:24 UTC


README

这是一个小型自动图像裁剪器集合。

要求

  • PHP 7.2+ 并安装 Imagick 扩展

描述

此项目包含三个功能图像裁剪器

CropEntropy

这是默认的裁剪方式。

// Use the CropEntropy class directly
$croppedImage = (new CropEntropy)->onFile('image.jpg')->getResults(200, 200); // \Imagick image
$croppedImage->writeimage('image-cropped.jpg');

或者

// Use the Trim class and explicit cut type
$croppedImage = Trim::makeCut('entropy')->onFile('image.jpg')->getResults(200, 200); // \Imagick image
$croppedImage->writeimage('image-cropped.jpg');

或者

// Use the Trim class and implicit (default) cut type
$croppedImage = Trim::makeCut()->onFile('image.jpg')->getResults(200, 200); // \Imagick image
$croppedImage->writeimage('image-cropped.jpg');

此类算法在图片中寻找具有最大“能量”的位置。在图像中,“能量”(或熵)由“边缘”来定义。例如,天空的图片边缘较少,而蚁巢的图片边缘很多。

在这种情况下,能量的计算方式如下

  1. 将图像转换为黑白
  2. 运行边缘过滤器,使我们只剩下边缘。
  3. 找到具有最高熵(即最多边缘)的部分
  4. 返回坐标,确保图片的这一部分不被裁剪掉

CropCenter

// Use the CropCenter class directly
$croppedImage = (new CropCenter)->onFile('image.jpg')->getResults(200, 200); // \Imagick image
$croppedImage->writeimage('image-cropped.jpg');

或者

// Use the Trim class and explicit cut type
$croppedImage = Trim::makeCut('center')->onFile('image.jpg')->getResults(200, 200); // \Imagick image
$croppedImage->writeimage('image-cropped.jpg');

这是最基本的裁剪技术

  1. 找到图像的精确中心
  2. 裁剪掉任何大于目标宽度和高度的边缘

CropBalanced

// Use the CropBalanced class directly
$croppedImage = (new CropBalanced)->onFile('image.jpg')->getResults(200, 200); // \Imagick image
$croppedImage->writeimage('image-cropped.jpg');

或者

// Use the Trim class and explicit cut type
$croppedImage = Trim::makeCut('balanced')->onFile('image.jpg')->getResults(200, 200); // \Imagick image
$croppedImage->writeimage('image-cropped.jpg');

CropBalanced 是 CropEntropy 的变体,我试图使其裁剪更加平衡。

  1. 将图像分成四个相等的正方形
  2. 找到每个正方形中最具活力的点
  3. 找到所有正方形图像加权平均兴趣点