gpt4 book ai didi

PHP - 替换图像中的颜色

转载 作者:可可西里 更新时间:2023-11-01 13:14:08 27 4
gpt4 key购买 nike

希望有人能帮忙,

我已经制作了一个 mask 图像的脚本......但是它依赖于 mask 的颜色('绿屏'风格)。麻烦的是,如果我遮蔽的图像包含该颜色,它就会被破坏。

我要做的是在 mask 图像之前用类似的颜色(例如 0,0,254)替换任何出现的键控颜色 (0,0,255)。

我找到了一些基于 gif 或 256 色 PNG 的解决方案,因为它们已被编入索引..

所以我的问题也是将它转换为 gif 或 256 png 然后查看索引并替换颜色或搜索每个像素并替换颜色是否更有效。

谢谢,

最佳答案

您需要打开输入文件并扫描每个像素以检查色键值。

像这样:

// Open input and output image
$src = imagecreatefromJPEG('input.jpg') or die('Problem with source');
$out = ImageCreateTrueColor(imagesx($src),imagesy($src)) or die('Problem In Creating image');

// scan image pixels
for ($x = 0; $x < imagesx($src); $x++) {
for ($y = 0; $y < imagesy($src); $y++) {
$src_pix = imagecolorat($src,$x,$y);
$src_pix_array = rgb_to_array($src_pix);

// check for chromakey color
if ($src_pix_array[0] == 0 && $src_pix_array[1] == 0 && $src_pix_array[2] == 255) {
$src_pix_array[2] = 254;
}


imagesetpixel($out, $x, $y, imagecolorallocate($out, $src_pix_array[0], $src_pix_array[1], $src_pix_array[2]));
}
}


// write $out to disc

imagejpeg($out, 'output.jpg',100) or die('Problem saving output image');
imagedestroy($out);

// split rgb to components
function rgb_to_array($rgb) {
$a[0] = ($rgb >> 16) & 0xFF;
$a[1] = ($rgb >> 8) & 0xFF;
$a[2] = $rgb & 0xFF;

return $a;
}

关于PHP - 替换图像中的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1548534/

27 4 0
文章推荐: javascript - onmouseup 事件未触发
文章推荐: php - 解析 PHP 中的类、函数和参数
文章推荐: html -
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com