PHP GD 调色板颜色
在PHP GD中,如何将真彩色图像转换为调色板而不丢失任何 > 颜色。使用 imagetruecolortopallete
它不起作用。我有一个函数可以遍历所有颜色并过滤它们(例如灰度)。并且它不保留所有颜色,例如这张兰博基尼的图片
- 图片
这是我的代码
$im = imagecreatefrompng("lamborghini.png");
$degrees = 0;
$img = imagecreatetruecolor(imagesx($im), imagesy($im));
imagecopy($img, $im, 0, 0, 0, 0, imagesx($im), imagesy($im));
imagetruecolortopalette($img, true, 256);
$t = imagecolorstotal($img);
for ($i = 0; $i < $t; $i++) {
$rgb = imagecolorsforindex($img, $i);
$hsv =rgbtohsv($rgb['red'], $rgb['green'], $rgb['blue']);
$h = $degrees;
$s = $hsv['s'];
$v = $hsv['v'];
while ($h > 360) {$h -= 360;};
$nrgb = hsvtorgb($h, $s, $v);
imagecolorset($img, $i, $nrgb['r'], $nrgb['g'], $nrgb['b']);
}
imagecopy($im, $img, 0, 0, 0, 0, imagesx($img), imagesy($img));
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
,它看起来像这样
您可以看到它失去了颜色。有任何解决方案吗?
另外,我不认为这与我的代码有关,而是如何 imagetruecolortopalette
输出它
In PHP GD how can you convert a truecolor image to a palette without losing any colors. With imagetruecolortopallete
it doesn't work. I have a function that runs through all the colors and filters them (eg. grayscale). and It doesn't retain all colors, such as this picture of a Lamborghini-
Picture
This is my code
$im = imagecreatefrompng("lamborghini.png");
$degrees = 0;
$img = imagecreatetruecolor(imagesx($im), imagesy($im));
imagecopy($img, $im, 0, 0, 0, 0, imagesx($im), imagesy($im));
imagetruecolortopalette($img, true, 256);
$t = imagecolorstotal($img);
for ($i = 0; $i < $t; $i++) {
$rgb = imagecolorsforindex($img, $i);
$hsv =rgbtohsv($rgb['red'], $rgb['green'], $rgb['blue']);
$h = $degrees;
$s = $hsv['s'];
$v = $hsv['v'];
while ($h > 360) {$h -= 360;};
$nrgb = hsvtorgb($h, $s, $v);
imagecolorset($img, $i, $nrgb['r'], $nrgb['g'], $nrgb['b']);
}
imagecopy($im, $img, 0, 0, 0, 0, imagesx($img), imagesy($img));
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
And it looks like this
You can see it loses colors. Is there any solution?
Also I don't think it has to do with my code but how imagetruecolortopalette
outputs it
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
检查
imagecolorstotal
上的返回值,无论您将要抖动的颜色数设置为多高,您始终会得到 256 种颜色作为返回值。 PNG-8& GIF 格式仅支持最多 256 种颜色的调色板。因此,即使您可以在调色板中使用超过 256 个颜色,您也必须将其保存为真实颜色才能供任何人使用,从而使整个转换过程浪费时间。换句话说,imagetruecolortopallete
的上限为 256 种颜色,不能再更高了。以下是如何以真彩色实现这一点,尽管它会占用大量资源。如果你想更有效地做到这一点,也许可以看看 imagemagick。
编辑:添加 rgbtohsv & hsvtorgb 函数。这些函数不是我写的。
Check the return on
imagecolorstotal
, you are always getting 256 colors as the return no matter how high you set the number of colors to be dithered to. PNG-8 & GIF formats only support palettes of up to 256 colors. So even if you can use more than 256 in a palette, you'd have to save it back as true color for anyone to be able to use it, thus making the whole conversion process a waste of time. In other wordsimagetruecolortopallete
has an upper limit of 256 colors, you cannot go higher.Here's how you can do it in truecolor, though it's resource intensive. Maybe look into imagemagick if you want to do this more efficiently.
Edit: Adding rgbtohsv & hsvtorgb functions. I did not write these functions.
将真彩色转换为调色板总是会导致颜色丢失,除非您的调色板表足够大以容纳图像中的每种独特颜色。仅兰博基尼就可能有超过(比如说)255 种黄色色调,然后您仍然需要调色板插槽来容纳除此之外的图像的其余部分。这就是为什么有真彩色图像。没有调色板,但每个像素都可以有自己专用的 RGB 三元组,以更准确地表示原始场景。
话虽这么说,我看不出转换真彩色图片如何将黄色变为红色,如示例图像中所示。您到底是如何进行调色板转换的?对每个像素进行采样并对其进行某种转换?
converting truecolor to palette is always going to involve losing colors, unless your pallette table is big enough to hold every unique color in the image. The lamborghini may very well have more than (say) 255 shades of yellow alone, and then you still need palette slots for the rest of the image on top of that. That's why there's truecolor images. There's no palette, but each pixel can have its own dedicated RGB triplet to more accurately represent the original scene.
That being said, I can't see how converting a truecolor picture will change yellow to red as in your sample images. How exactly are you doing this palette conversion? Sampling each pixel and doing some sort of transformation on it?
这个功能怎么样: http://www.php.net/manual/en /function.imagetruecolortopalette.php。不过,可能需要对颜色数量进行一些调整才能得到正确的结果。
How about this function: http://www.php.net/manual/en/function.imagetruecolortopalette.php. It might take some messing with the number of colors to get it right, though.