如何使用 PHP 或 Ruby 从图像中删除某些颜色?

问题描述:

假设有 3 个圆圈:红色、蓝色、黑色.

Say there are 3 circles: red, blue, black.

我只想保留黑色圆圈.如何去除红色和蓝色圆圈?

I only want the black circle to remain. How can I remove the red and blue circles?

既然您已经要求了 PHP 解决方案:

Since you have asked for a PHP solution:

  • 首先使用 imagecreatefrompng 或其他类似功能加载您的图片图片格式
  • 之后,使用 imagesximagesy 获取图片的大小.
  • 现在,您可以通过

  • First load your picture with imagecreatefrompng or the similar functions for other image formats
  • Afterwards, use imagesx and imagesy to get the size of the image.
  • Now, what you can loop over all pixels via

for ($i = 0; $i < $imageWidth; $i++) {
    for ($j = 0; $j < $imageHeight; $j++) {
        // check color and replace
    }
}

  • 最后,使用 imagecolorat 获取颜色(检查它是否在特定的范围内,不要将黑色作为一种好颜色,还要将所有在红色、绿色的每个值上 >= 250 的颜色都视为一种好颜色和蓝色为例)

  • Finally, use imagecolorat to get the color (check if it is in a specific range, don't take only black as a good color, but also all colors that have >= 250 at each value of red, green and blue for example)