如何在 Prettytable 中删除值小于 0% 的行
Posted
技术标签:
【中文标题】如何在 Prettytable 中删除值小于 0% 的行【英文标题】:How to remove rows with value less than 0% in Prettytable 【发布时间】:2021-02-11 09:44:27 【问题描述】:我正在尝试拒绝。由于循环遍历每个像素,图像中的颜色数量会显示几种颜色,其值小于 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
【问题讨论】:
【参考方案1】:您一开始就不添加它们怎么样?此外,您正在寻找等于或小于零的值。
for color, count in Counter(colors).items():
percent = int(count/total * 100)
if percent > 0:
pt.add_row([color, count, percent])
【讨论】:
以上是关于如何在 Prettytable 中删除值小于 0% 的行的主要内容,如果未能解决你的问题,请参考以下文章
Hive 任务卡在 map = 0%, reduce = 0%