drtkyykai004574380 2014-03-11 14:22
浏览 48
已采纳

php图像过滤器“colorize”破坏图像细节

i am trying to to colorize a black and white picture on my server.

i have tried doing this with multiple methods and the only one that kind-of-works is gd imagefilter via filter IMG_FILTER_COLORIZE.

it does kind of colorize the image in exactly the same color that i want, but it loses all the details on the image, as if it just trimmed black dots that were not black enough and thinned all the black lines, making them almost invisible. here is what i'm talking about:

original imagecolorized picture

this result was achieved with this code

$im=imagecreatefromjpeg($orig_file);
imagefilter($im, IMG_FILTER_COLORIZE, 71, 92, 10);
imagejpeg($im, $output_file, 95);

why is this happening? are there any other methods how i could colorize the image? my original image is quite large and i can't iterate over it as it is too slow; that's why i'm trying to use a library that would do this

  • 写回答

1条回答 默认 最新

  • doupu1949 2014-03-15 13:49
    关注

    i have managed to achieve the desired result with help of Imagick and compositeImage. here is the result

    original picturedesired result

    how i achieved it is kind of a trick that works only in a very specific condition- which is the need to have the background white and only black/gray objects in front (or the exact opposite). this technique wouldn't work on a background with transparency

    the idea is to have 2 layers- underneath is the layer with original grayscale image and the top layer- fully filled with the desired color. then you composite these images using "COMPOSITE_LIGHTEN" (you might use other methods, like darken for example- if you have white objects on a black background)

    this is the code

    $base_color="#475c0a";
    $im=new Imagick($orig_file);
    
    $im2 = new Imagick();
    $im2->newImage($im->getImageWidth(), $im->getImageHeight(), new ImagickPixel($base_color) );
    
    $im->compositeImage($im2, Imagick::COMPOSITE_LIGHTEN, 0, 0);
    
    $im->writeImage($new_image_path);
    

    hope this will help someone out

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记