【问题标题】:Editing image colors in PHP - color exchange在 PHP 中编辑图像颜色 - 颜色交换
【发布时间】:2015-09-12 20:03:12
【问题描述】:

为了具体描述我的问题,我正在尝试将所有接近黑色的颜色(在阈值内)转换为完全黑色。例如,在 RGB 术语中,所有分量小于 50 的颜色变为 (0,0,0)。我知道这可以通过下面的链接在 GIMP 中完成,但是有人知道这可以在 PHP 中完成吗?

https://askubuntu.com/questions/27578/in-gimp-can-i-change-all-black-pixels-in-a-picture-to-blue-pixels

【问题讨论】:

标签: php image imagemagick gd


【解决方案1】:

您可以使用 GD 库来读取和设置图像中的像素颜色。确切的算法和阈值由您决定。

Read a Pixel

Set a pixel

【讨论】:

    【解决方案2】:

    我不是 PHP 程序员,但它看起来像这样:

    #!/usr/local/bin/php -f
    
    <?php
    $im = imagecreatefrompng("image.png");
    $black = imagecolorallocate($im,0,0,0); 
    $w = imagesx($im); // image width
    $h = imagesy($im); // image height
    for($x = 0; $x < $w; $x++) {
       for($y = 0; $y < $h; $y++) {
          // Get the colour of this pixel
          $rgb = imagecolorat($im, $x, $y);
    
          $r = ($rgb >> 16) & 0xFF;
          if($r>=50)continue;         // Don't bother calculating rest if over threshold
    
          $g = ($rgb >> 8) & 0xFF;
          if($g>=50)continue;         // Don't bother calculating rest if over threshold
    
          $b = $rgb & 0xFF;
          if($b>=50)continue;         // Don't bother calculating rest if over threshold
    
          // Change this pixel to black
          imagesetpixel($im,$x,$y,$black);
       }
    }
    imagepng($im,"result.png");
    ?>
    

    哪个转换了这个

    到这里

    【讨论】:

      猜你喜欢
      • 2017-11-12
      • 2010-10-17
      • 2010-12-05
      • 2010-12-09
      • 2012-08-24
      • 1970-01-01
      • 2011-06-23
      • 2012-10-28
      • 1970-01-01
      相关资源
      最近更新 更多