【问题标题】:How to find if a specific pixel of an image has a higher Red, Green, or Blue value in Python如何在 Python 中查找图像的特定像素是否具有更高的红色、绿色或蓝色值
【发布时间】:2021-08-02 02:30:23
【问题描述】:

我试图找出图像的某个像素是否具有更多的红绿或蓝值。我在下面尝试了这段代码:

im = Image.open("image.png")
x = 32
y = 32

pixel = im.load()

rgb = pixel[x,y]
rgb = int(rgb)

if rgb[0] > rgb[1,2]:
    print("Red")
elif rgb[1] > rgb[0,2]:
    print("Green")
elif rgb[2] > rgb[0,1]:
    print("Blue")

但它给了我这个错误:

  File "d:\path\app.py", line 11, in <module>
    rgb = int(rgb)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'tuple'

请让我知道我做错了什么,或者是否有更好的方法!

谢谢
-丹尼尔

【问题讨论】:

  • 也许你的意思是pixel((x,y))
  • int() 不接受元组参数。如果 rgb 是一个元组,不要管它。然后使用` if rgb[0] > rgb[1] and rgb[0]>rgb[2]` etc...
  • 尝试 rgb = [int(i) for i in rgb] 将 rgb 列表转换为整数

标签: python image python-imaging-library rgb


【解决方案1】:

你可以这样做:

red_image = Image.open("image.png")
red_image_rgb = red_image.convert("RGB")
rgb_pixel_value = red_image_rgb.getpixel((10,15))

if rgb_pixel_value[0]>rgb_pixel_value[1] and rgb_pixel_value[0]>rgb_pixel_value[2]:
    print("Red")
elif rgb_pixel_value[0]<rgb_pixel_value[2] and <rgb_pixel_value[1]<rgb_pixel_value[2]:
    print("Blue")
else:
    print("Green")

这是一个互动程序:

from PIL import Image, ImageTk
import tkinter as tk
from tkinter import filedialog
root=tk.Tk()
def select_image():
    s=filedialog.askopenfilename(filetypes=(("PNG",'*.png'),("JPEG",'*.jpg')))
    if s!='':
        global red_image
        red_image = Image.open(s)
        image1=ImageTk.PhotoImage(file=s)
        lbl1.config(image=image1)
        lbl1.image=image1
        root.bind("<Motion>",check_pixel)
def check_pixel(event):
    red_image_rgb = red_image.convert("RGB")
    rgb_pixel_value = red_image_rgb.getpixel((event.x,event.y))
    lbl2.config(text=f"Red:   {rgb_pixel_value[0]}        Green:   {rgb_pixel_value[1]}        Blue:   {rgb_pixel_value[2]}")
    if rgb_pixel_value[0]>rgb_pixel_value[1] and rgb_pixel_value[0]>rgb_pixel_value[2]:
        lbl3.config(text="Red",fg="Red")
    elif rgb_pixel_value[0]<rgb_pixel_value[2]and rgb_pixel_value[1]<rgb_pixel_value[2]:
        lbl3.config(text="Blue",fg="Blue")
    else:
        lbl3.config(text="Green",fg="green")

button1=tk.Button(root,text='Select Image',command=select_image)
button1.pack()
lbl1=tk.Label(root)
lbl1.pack()
lbl2=tk.Label(root,text="Red:        Green:        Blue:")
lbl2.pack(side=tk.BOTTOM)
lbl3=tk.Label(root)
lbl3.pack()
root.mainloop()

【讨论】:

  • 请注意,如果我们有 R, G, B = 3, 1, 2,那么 R 是三个中最大的,但是 R > G > B 是 False。
【解决方案2】:

你在这里做错的是你在int()函数中传递了一个tuple,它返回一个TypeError

更好的方法是使用 Opencv 在给定的 (x, y) 处获取 R、G 和 B 的值

import cv2

im = cv2.imread("image.png")

x, y = 32, 32


#get r, g and b values at (x, y)
r, g, b = im[x, y]
print(f"Pixel at ({x}, {y}) - Red: {r}, Green: {g}, Blue: {b}")

#then do your comparison
#following is an example
if (r>=g) and (r>=b):
    print("Red")
elif (g>=r) and (g>=b):
    print("Green")
else:
    print("Blue")
    

【讨论】:

    猜你喜欢
    • 2017-08-19
    • 2011-10-28
    • 2011-09-22
    • 2022-12-06
    • 2022-12-19
    • 2018-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多