python - 如何在 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,但它不起作用
解决方案
颜色应该在除数之前。
所以与其:
($ red/0.299)+($ green/0.587)+($ blue/0.114)
推荐阅读
- python - Django:访问多对多中的额外字段(通过=)
- angular - 如何摆脱 WebStorm 中的 Angular 模板空间亮点
- c - 在 C 函数中拆分一个 int 数组并返回 2 个不同的数组
- php - 如何使用 laravel 在更新时从公用文件夹中删除旧图像
- selenium - github 在每个 selenium-webdriver 测试中要求验证码
- bash - 具有浮点值的变量的减法不起作用
- postgresql - 在定期更改数据库密码时实现连接重建机制
- c# - 如何从 HttpResponseMessage 中检索 Cookie
- delphi - 如何使用“for/in”在另一个组件中查找组件?
- scala - 如何使用设计模式或 OOP 编写 scala spark 代码以连接到多个云平台(Azure、AWS)