【问题标题】:Python - Checking for color in the center of an imagePython - 检查图像中心的颜色
【发布时间】:2013-05-31 00:22:53
【问题描述】:

您好,我想知道如何检查图像中心的平均颜色并从网络摄像头获取图像。

我查看了 PIL,但它似乎无法访问网络摄像头来抓取照片,或者有可能吗?

我找到了这段代码

im = Image.open('image.gif')
rgb_im = im.convert('RGB')
r, g, b = rgb_im.getpixel((60, 80))

print r, g, b

这将检查单个像素。我是 python 新手,想知道如何做一个循环来检查一个 20x20 像素的正方形,然后平均 RGB。

还有确定颜色的简单方法吗?
(我现在正在用 RGB 做一些 if > 和

【问题讨论】:

    标签: image colors python-imaging-library


    【解决方案1】:
    1. 裁剪所需图像的中心部分
    2. 选择裁剪部分。
    3. 使用下面的代码找到它的颜色

      result = cropped_image.convert('P', palette=Image.ADAPTIVE, colors=5)

    这会输出使用的 5 种颜色。您可以将 colors=1 更改为一种颜色。 您现在可以使用Image.quantize 方法。

    类似的答案:How do I convert any image to a 4-color paletted image using the Python Imaging Library?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-28
      • 1970-01-01
      • 2018-07-16
      • 2017-02-04
      • 2011-01-17
      • 2022-06-13
      • 1970-01-01
      相关资源
      最近更新 更多