naomai/php-dominantcolor

此包最新版本(dev-master)没有提供许可信息。

使用 K-means 聚类算法在图像中寻找主色

dev-master 2020-06-12 12:52 UTC

This package is auto-updated.

Last update: 2024-09-27 21:41:50 UTC


README

此库从图像中提取主色 - 那些最有可能吸引观众眼睛的颜色。

Album arts example

该库使用 K-means 聚类 方法将图像像素分组到不同的 颜色桶 中。然后通过饱和度和与其他颜色之间的差异评估这些颜色。从中选择两种作为 最突出 的(主色和次色)。

用法

我们想从图像 heart.png 中找到最突出的颜色以及三种额外的颜色。

$imageSrc = "heart.png";
$totalNumberOfColors = 5; // primary + secondary + 3 additional

$colorInfo = Naomai\DominantColor::fromFile($imageSrc, $totalNumberOfColors);

颜色的数量是颜色桶的数量,它还应包括主色和次色。

结果是以下结构的数组

$colorInfo = [
	'primary' => 0xDEFDEF, 
	'secondary' => 0xABCABC,
	'palette' => [
		0 => ['color' => 0xABCABC, 'score' => 0.8],
		/// etc
	]

所有颜色都按照标准 RGB 整数格式化:0xRRGGBBpalette 包含额外的颜色。 score 字段是从次色选择算法得到的分数。所选的次色值为 1.0

请查看 examples 目录以查看视觉演示。

此项目依赖于 Benjamin Delespierre 的优雅 PHP K-Means 库。