How to to remove certain colors from an image with PHP or Ruby?

前端 未结 3 1760
南方客
南方客 2021-01-01 07:56

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?

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-01 08:23

    Since you have asked for a PHP solution:

    • 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
          }
      }
      
    • 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)

    • ... and imagecolorset to set the color
    • Now you can save the image using imagepng for example.

提交回复
热议问题