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条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料