首页 > 解决方案 > 如何在 PHP 或 Python 中将灰度图像恢复/转换为 RGB 图像

问题描述

我已成功将图像转换为灰度,我想将灰度图像还原为 RGB 图像。请帮忙。提前致谢。这是我将图像转换为灰度的代码


$gdlogo = imagecreatetruecolor($width_logo, $heigth_logo);
for((int) $start_x=0;$start_x<$width_logo;$start_x++){
    for((int) $start_y=0;$start_y<$heigth_logo;$start_y++){      
        $color_index = imagecolorat($imglogo, $start_x, $start_y);
        $color_tran = imagecolorsforindex($imglogo, $color_index);
        $red = $color_tran["red"];
        $green = $color_tran["green"];
        $blue = $color_tran["blue"];
        $image_get[$start_x][$start_y] =$red.$green.$blue;
        $colors= (0.299*$red) + (0.587*$green) + (0.114*$blue);
        $color = imagecolorallocate($gdlogo,$colors[0],$colors[1],$colors[2]); 
        imagesetpixel($gdlogo,$start_x,$start_y, $color);
    }
}

在那里,我看到了一种通过乘以颜色索引来改变灰度图像的方法:(0.299 * $ red) + (0.587 * $ green) + (0.114 * $ blue); 所以它变成灰色我认为如果颜色指数除以(0.299 / $ red)+(0.587 / $ green)+(0.114 / $ blue);它将返回到 RGB,但它不起作用

标签: pythonphp

解决方案


颜色应该在除数之前。

所以与其:

($ red/0.299)+($ green/0.587)+($ blue/0.114)

推荐阅读