問題描述
希望有人能幫忙
我制作了一個屏蔽圖像的腳本......但是它依賴于一種顏色來屏蔽(綠屏"樣式).問題是,如果我屏蔽的圖像包含那種顏色,它就會被破壞.
I have made a script that masks images... however it is reliant on a colour to mask with ( 'green screen' style). The trouble is if the image that I am masking contains that colour it is ruined.
我要做的是在屏蔽圖像之前用類似的顏色(例如 0,0,254)替換任何出現的鍵控顏色 (0,0,255).
What I am looking to do is prior to masking the image replace any occurance of my keying colour (0,0,255) with a similar colour such as 0,0,254.
我找到了一些基于 gif 或 256 色 PNG 的解決方案,因為它們已編入索引..
I have found a few solutions based around gif's or 256 colour PNG as they are indexed..
所以我的問題也是將其轉換為 gif 或 256 png 然后查看索引并替換顏色或搜索每個像素并替換顏色是否更有效.
So my question is also will it be more efficient to convert it to a gif or 256 png then look through the index and replace the colour or search through every pixel and replace the colours.
謝謝,
推薦答案
您需要打開輸入文件并掃描每個像素以檢查色鍵值.
You need to open the input file and scan each pixel to check for your chromokey value.
像這樣:
// 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 - 替換圖像中的顏色的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!