如何使用 PHP 或 Ruby 从图像中删除某些颜色? [英] How to to remove certain colors from an image with PHP or Ruby?

查看:61
本文介绍了如何使用 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)

    这篇关于如何使用 PHP 或 Ruby 从图像中删除某些颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆