drccfl9407 2018-01-09 14:16
浏览 185
已采纳

在图像中用透明替换多个颜色

i have a rain radar image here with big grey and white parts as backgroound. i need to remove the background ( grey / white ) and make in transparent. but it does not work. i already tried

   // replace white
   $rgb = imagecolorexact($im, 255, 255, 255);
   imagecolortransparent($im, $rgb);
   // replace grey
   $rgb = imagecolorexact($im, 189, 189, 189);
   imagecolortransparent($im, $rgb);

But this is not working. Its only one part transparent ( either the white one or the grey one ). I cannot remove both colors at the same time.

I really have no idea how images work .. so please if you know a way to achieve what i want please let me know.

Thanks

enter image description here

  • 写回答

2条回答 默认 最新

  • doushenyi9104 2018-01-09 15:40
    关注

    First, make all the grey pixels white. Then make all the white pixels transparent. That's it. Read it again :-)

    Refer to PHP GD documentation to check actual parameters and details.

    // Load up the original image
    $src=imagecreatefrompng('weather.png');
    
    // Ensure image is palettised
    if(imageistruecolor($src)){
       imagetruecolortopalette($src);
    }
    
    // Find nearest colours to white and grey 189
    $whiteindex=imagecolorclosest($src,255,255,255);
    $greyindex =imagecolorclosest($src,189,189,189);
    
    // Make all greys white and all nearly whites white, and both transparent
    imagecolorset($src,$greyindex,255,255,255,127);
    imagecolorset($src,$whiteindex,255,255,255,127);
    
    // Write result 
    imagepng($src,"result.png");
    

    Note that the code you started with, and the code above is using the GD library that comes ready-installed with most PHP interpreters. You could, instead, use the IMagick library (which is the PHP binding of ImageMagick) which is much more comprehensive. Your code would become something like:

    // Move to a format which supports transparency
    $imagick->setimageformat('png');
    
    // Set $color to white first
    $imagick->transparentPaintImage($color, $alpha, 10 * \Imagick::getQuantum(),false);
    
    // Set $color to grey first
    $imagick->transparentPaintImage($color, $alpha, 10 * \Imagick::getQuantum(),false);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭