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 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历