我需要用PHP创建调色板。我使用这样的代码:
for ($x = 0; $x < $this->width; $x += $level) {
for ($y = 0; $y < $this->height; $y += $level) {
$index = imagecolorat($this->workingImage, $x, $y);
$rgb = imagecolorsforindex($this->workingImage, $index);
$color = $this->getClosestColor($rgb["red"], $rgb["green"], $rgb["blue"]);
$hexarray[] = $this->RGBToHex($color[0], $color[1], $color[2]);
}
}

我该怎么做呢?对于任何图片,我至少需要10种颜色。我试图使用另一种方法,像素图像之前,收到调色板,但这对我没有帮助。
发布于 2016-04-01 14:02:18
不幸的是,您将初始图像与不正确的结果和所期望的结果混为一谈,因此我将其分隔为下面的image.png:

现在,我计划使用pnmquant,它是NetPBM包的一部分,可以预编译,可以从SourceForge安装在大多数平台上。
因此,首先,我需要将您的PNG转换为NetPBM的PPM格式,这样pnmquant工具才能工作--为此我将使用pngtopnm。然后,我想把颜色提取成一个样本,然后放大样本,这样你就可以看到比提取的21个像素更大的东西--我会用ImageMagick来表示它,我想对它做一个文本表示,这样你就可以看到数字,我会用ImageMagick来表示它。
pngtopnm image.png | pnmquant 21 | pnmtopng > result.png

convert image.png pnm:- | pnmquant 21 | convert - -unique-colors -scale 400x swatch.png

convert image.png pnm:- | pnmquant 21 | convert - -unique-colors -depth 8 txt:
# ImageMagick pixel enumeration: 21,1,65535,srgb
0,0: (65278,9766,2827) #FE260B srgb(254,38,11)
1,0: (65278,15163,2570) #FE3B0A srgb(254,59,10)
2,0: (65535,10794,5140) #FF2A14 srgb(255,42,20)
3,0: (65535,11565,5140) #FF2D14 srgb(255,45,20)
4,0: (65278,11308,5140) #FE2C14 srgb(254,44,20)
5,0: (65535,11822,5140) #FF2E14 srgb(255,46,20)
6,0: (65278,11822,5140) #FE2E14 srgb(254,46,20)
7,0: (65278,19532,3341) #FE4C0D srgb(254,76,13)
8,0: (65278,20817,3855) #FE510F srgb(254,81,15)
9,0: (65278,16962,5140) #FE4214 srgb(254,66,20)
10,0: (65535,20303,4369) #FF4F11 srgb(255,79,17)
11,0: (65278,20817,4626) #FE5112 srgb(254,81,18)
12,0: (63479,33410,28270) #F7826E srgb(247,130,110)
13,0: (62451,56540,55769) #F3DCD9 srgb(243,220,217)
14,0: (62708,61166,59110) #F4EEE6 srgb(244,238,230)
15,0: (62451,62708,62965) #F3F4F5 srgb(243,244,245)
16,0: (65278,63736,62965) #FEF8F5 srgb(254,248,245)
17,0: (62194,65278,65535) #F2FEFF srgb(242,254,255)
18,0: (65278,64250,63736) #FEFAF8 srgb(254,250,248)
19,0: (65535,65535,65535) #FFFFFF white
20,0: (65278,65278,65278) #FEFEFE srgb(254,254,254)对于PHP,您可以将上面的命令行工具打包并执行,或者查看源代码并适应PHP。
https://stackoverflow.com/questions/36236111
复制相似问题