简体   繁体   English

在 PHP 中从图像生成调色板

[英]Generate color palette from image in PHP

I need to create color palette with PHP.我需要用 PHP 创建调色板。 I use code like this:我使用这样的代码:

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]);
    }
}

在此处输入图片说明

How can I do it?我该怎么做? For any picture I need at least 10 colors.对于任何图片,我至少需要 10 种颜色。 I am tried to use another methods, pixelate image before receiving color palette, but it is not helping for me.我试图使用另一种方法,在接收调色板之前对图像进行像素化,但这对我没有帮助。

Unfortunately you have mixed up your starting image with your incorrect results and your desired results, so I have separated it out as image.png below:不幸的是,您将起始图像与不正确的结果和所需的结果混合在一起,因此我将其分离为下面的image.png

在此处输入图片说明

Now, I plan to use pnmquant which is part of the NetPBM package which is available precompiled and ready to install on most platforms from SourceForge.现在,我计划使用pnmquant ,它是 NetPBM 包的一部分,它可以预编译并准备安装在 SourceForge 的大多数平台上。

So, first I need to convert your PNG to NetPBM's PPM format for the pnmquant tool to work - I'll use pngtopnm for that.因此,首先我需要将您的PNG转换为 NetPBM 的PPM格式,以便pnmquant工具工作 - pngtopnm我将使用pngtopnm Then I want to extract the colours as a swatch and enlarge the swatch so you can see something bigger than the extracted 21 pixels -I'll use ImageMagick for that, and I want to make a text representation of it so you can see the numbers, and I'll use ImageMagick for that.然后我想提取颜色作为样本并放大样本,这样你就可以看到比提取的 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)

As regards PHP, you can shell out and exec the command-line tools above, or look at the source code and adapt to PHP.关于PHP,可以shell out exec 上面的命令行工具,或者看源码,适应PHP。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM