使用 Python 计算 RGBA 值
Counting RGBA values with Python
我读取了一张图像,将 RGBA 值推入了一个数组,现在我想计算某些颜色的出现次数。但是我得到的只是 0。我该怎么做(不转换为字符串)?相关代码片段和输出:
输出:
Image123.png
8820
[(138, 18, 20, 255), (138, 18, 20, 255), (138, 18, 20, 255), (138, 18, 20, 255), (138, 18, 20, 255), (138, 18, 20, 255), (138, 18, 20, 255), (138, 18, 20, 255), (138, 18, 20, 255), (138, 18, 20, 255), (138, 18, 20, 255), (138, 18, 20, 255), (138, 18, 20, 255), (138, 18, 20, 255), (138, 18, 20, 255), (138, 18, 20, 255), (138, 18, 20, 255), (138, 18, 20, 255), (138, 18, 20, 255), ......
0
0
代码:
read_pixel = []
print(filename)
read_pixel.append(pixel[image_x, image_y])
print(image_size_x*image_size_y)
print(read_pixel)
count_lte_70_1 = read_pixel.count("(138, 18, 20, 255)")
print(count_lte_70_1)
#without parenthesis
count_lte_70_2 = read_pixel.count("138, 18, 20, 255")
print(count_lte_70_2)
嗯,你不应该使用 count("(a,b,c,d)")
,而是 count((a,b,c,d))
您现在的做法是计算列表中字符串的数量
x=[(1,2),(3,4),(3,4)]
print(x.count((1,2)) #returns 1
print(x.count((3,4)) #returns 2
有
count_lte_70_1 = read_pixel.count("(138, 18, 20, 255)")
您正在搜索 字符串 ,而您的列表包含 元组 。相反,您应该使用:
count_lte_70_1 = read_pixel.count((138, 18, 20, 255))
这里的引号是你的问题,你正在搜索一个元组而不是一个字符串。只需留下引号并使用
read_pixel.count((138, 18, 20, 255))
我读取了一张图像,将 RGBA 值推入了一个数组,现在我想计算某些颜色的出现次数。但是我得到的只是 0。我该怎么做(不转换为字符串)?相关代码片段和输出:
输出:
Image123.png
8820
[(138, 18, 20, 255), (138, 18, 20, 255), (138, 18, 20, 255), (138, 18, 20, 255), (138, 18, 20, 255), (138, 18, 20, 255), (138, 18, 20, 255), (138, 18, 20, 255), (138, 18, 20, 255), (138, 18, 20, 255), (138, 18, 20, 255), (138, 18, 20, 255), (138, 18, 20, 255), (138, 18, 20, 255), (138, 18, 20, 255), (138, 18, 20, 255), (138, 18, 20, 255), (138, 18, 20, 255), (138, 18, 20, 255), ......
0
0
代码:
read_pixel = []
print(filename)
read_pixel.append(pixel[image_x, image_y])
print(image_size_x*image_size_y)
print(read_pixel)
count_lte_70_1 = read_pixel.count("(138, 18, 20, 255)")
print(count_lte_70_1)
#without parenthesis
count_lte_70_2 = read_pixel.count("138, 18, 20, 255")
print(count_lte_70_2)
嗯,你不应该使用 count("(a,b,c,d)")
,而是 count((a,b,c,d))
您现在的做法是计算列表中字符串的数量
x=[(1,2),(3,4),(3,4)]
print(x.count((1,2)) #returns 1
print(x.count((3,4)) #returns 2
有
count_lte_70_1 = read_pixel.count("(138, 18, 20, 255)")
您正在搜索 字符串 ,而您的列表包含 元组 。相反,您应该使用:
count_lte_70_1 = read_pixel.count((138, 18, 20, 255))
这里的引号是你的问题,你正在搜索一个元组而不是一个字符串。只需留下引号并使用
read_pixel.count((138, 18, 20, 255))