如何只打印一次值,而不是每个值? [英] How do I print values only once, instead of each single value?

查看:82
本文介绍了如何只打印一次值,而不是每个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个脚本来扫描图像,然后告诉我其中的颜色像素是什么,但是当我打印像素颜色值时,它每次出现在图像中时都会打印相同的颜色值.

I am trying to make a script that scans an image, and then tells me what color pixels are in it, but when I print the pixels color value, it prints the same color value for every time it occurs in the image.

我已经想出对值进行排序,它们彼此并列.

I have figured out to sort the values sort they are besides each other.

from PIL import Image


img = Image.open('images/image.png')

print(img.size)
print("pixels")

pix = img.load()
pix_val = list(img.getdata())

pix_val.sort()

print(pix_val)

每次出现在图像中时,它都会打印颜色值,所以我打印的很长.

It prints the color values for each time they occur in the image, so I get a very long print.

推荐答案

您可以使用以下代码获取唯一像素的分布:

You can get the distribution of unique pixels using the following code:

from collections import Counter
Counter(pix_val)

如果您只需要知道哪些是独特的颜色,只需像 Adrian 在评论中告诉您的那样运行以下代码即可.

If you only need to know which are the unique colours, just run the following code as Adrian told you on the comments.

set(pix_val)

这篇关于如何只打印一次值,而不是每个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆