Author: Roberto Aleman, ventics.com , license: GNU AGPLv3
Here is a simplified table to interpret entropy levels in the context of images:- 0 to 1 : Very low randomness (little variability in colors).
- 1 to 3 : Low to moderate randomness (little color diversity).
- 3 to 5 : Moderate to high randomness (acceptable color diversity).
- 5 to 7 : High randomness (good color diversity).
- 7 to 8 : Very high randomness (almost all colors are equally likely).
imagecreatefrompng() function. This allows the script to work with the image in a format that PHP can manipulate.
php
$this->image = imagecreatefrompng($imagePath);
php
$this->width = imagesx($this->image);
$this->height = imagesy($this->image);
imagecolorat() to get the color index of each pixel. The frequency of each color is stored in an array called $histogram.
php
for ($y = 0; $y < $this->height; $y++) {
for ($x = 0; $x < $this->width; $x++) {
$colorIndex = imagecolorat($this->image, $x, $y);
if (!isset($histogram[$colorIndex])) {
$histogram[$colorIndex] = 0;
}
$histogram[$colorIndex]++;
}
}
php
foreach ($histogram as $count) {
$probability = $count / $totalPixels;
if ($probability > 0) {
$entropy -= $probability * log($probability, 2);
}
}
php
if ($entropy < 1) {
return "Low entropy (little randomness)";
} elseif ($entropy < $maxEntropy / 2) {
return "Moderate entropy (acceptable level of randomness)";
} elseif ($entropy < $maxEntropy * 0.75) {
return "High entropy (good randomness)";
} else {
return "Very high entropy (high randomness)";
}
- Low Entropy: Indicates that the image has few colors or that the colors are very similar to each other, resulting in a less visually interesting image.
- High Entropy: Suggests that the image has a wide variety of colors, which can make it more attractive and complex.