如何在 Prettytable 中删除值小于 0% 的行

How to remove rows with value less than 0% in Prettytable

我正在努力获得号码。由于循环遍历每个像素,图像中的颜色显示了几种颜色,其值小于 0%。我正在尝试删除 Prettytable

中显示的以下列表中百分比值小于 0% 的行

代码如下:

img = Image.open("bnw.jpg")
size = w, h = img.size
data = img.load()

colors = []
for x in range(w):
    for y in range(h):
        color = data[x, y]
        hex_color = '#'+''.join([hex(c)[2:].rjust(2, '0') for c in color])
        colors.append(hex_color)

pt = prettytable.PrettyTable(['Color', 'Count', 'Percentage'])

total = w * h

for color, count in Counter(colors).items():
    percent = int(count/total * 100)
    pt.add_row([color, count, percent])


# res = []
# for i in colors:
#     if i not in res:
#         res.append(i)
# print("The list after removing duplicates : " + str(res))

print(pt, total)

这是输出

+---------+--------+------------+
|  Color  | Count  | Percentage |
+---------+--------+------------+
| #ffffff | 478329 |     71     |
| #fdfdfd |  932   |     0      |
| #fefefe |  1219  |     0      |
| #fbfbfb |  556   |     0      |
| #fafafa |  279   |     0      |
| #f8f8f8 |   89   |     0      |
| #f9f9f9 |  199   |     0      |
| #fcfcfc |  705   |     0      |
| #f7f7f7 |   50   |     0      |
| #040404 |  538   |     0      |
| #020202 |  883   |     0      |
| #010101 |  1196  |     0      |
| #000000 | 179583 |     26     |
| #080808 |   45   |     0      |
| #060606 |  176   |     0      |
| #050505 |  323   |     0      |
| #030303 |  726   |     0      |
| #0a0a0a |   17   |     0      |
| #070707 |   78   |     0      |
| #090909 |   28   |     0      |
| #f6f6f6 |   24   |     0      |
| #f5f5f5 |   12   |     0      |
| #0b0b0b |   8    |     0      |
| #f4f4f4 |   5    |     0      |
+---------+--------+------------+ 666000

这是我试图达到的要求输出

+---------+--------+------------+
|  Color  | Count  | Percentage |
+---------+--------+------------+
| #ffffff | 478329 |     71     |
| #000000 | 179583 |     26     |
+---------+--------+------------+ 666000

你一开始就不添加它们怎么样?此外,您正在寻找 等于 或小于零的值。

for color, count in Counter(colors).items():
    percent = int(count/total * 100)
    if percent > 0:
        pt.add_row([color, count, percent])