Python 3.x 显示图像的颜色
我正在尝试打印图像的颜色Python 3.x 显示图像的颜色,python-3.x,image-processing,pillow,Python 3.x,Image Processing,Pillow,我正在尝试打印图像的颜色 import math from PIL import Image im = Image.open("test.jpg").convert("RGBA") color_count = 10 # im.rotate(45).show() colors = im.getcolors(maxcolors=1000000) max_occurence, most_present = 0, 0 for c in colors: if c[0] > max_occur
import math
from PIL import Image
im = Image.open("test.jpg").convert("RGBA")
color_count = 10
# im.rotate(45).show()
colors = im.getcolors(maxcolors=1000000)
max_occurence, most_present = 0, 0
for c in colors:
if c[0] > max_occurence:
(max_occurence,most_present) = c
print(most_present)
我能够打印下面图片的像素。是否有一种方法可以提取颜色,如黄色、红色、绿色、白色等作为输出,而不是像素
在获得像素信息后,如何打印像素的颜色
比如说<代码>[(159,168,210),(23,21,22),(143,107,73),(235,226,199),(120,122,152),(211,166,102),(82,62,54),(85,40,37),(179,150,101)]如何将其表示或打印为[红,绿,黄,蓝,黑]
等
谢谢
Ankush Reddy.您可以使用“从集合”来计算所有颜色:
from collections import Counter
import random
# c = Counter(im.getcolors())
# print(c.most_common())
def t() : # using a 3 tuple instead of image data, you plug your image in
return ( random.randint(0,10), random.randint(0,10), random.randint(0,10))
pixel = [t() for _ in range(2000)]
d = Counter(pixel) # you stuff your colors-list in here
for i in d.most_common(10): # get the 10 most often occuring ones
print(*i)
但这只会检测到“精确的”重复像素
输出:
(5, 1, 5) 6
(6, 5, 7) 6
(8, 4, 7) 6
(3, 5, 4) 6
(4, 5, 2) 6
(0, 7, 1) 5
(7, 8, 8) 5
(1, 7, 1) 5
(0, 7, 10) 5
(10, 7, 0) 5
对于人眼而言,(10,7,10)
和(11,9,10)
之间的差异是可以忽略的-它是“相同”的颜色。要检测所有的“白色”像素,你需要有某种“分组”功能,告诉你这个像素是否与周围的其他像素大致相同——欢迎来到计算机视觉世界
据我所知,(r,b,g)=白色,(r,b,g)=黄色没有简单的映射-你必须自己找到(r,g,b)的范围
RGB id对于这种操作来说是相当不适合的颜色空间,HSV(色调饱和度值)可能更适合:-紧密相连的色调是“相似”的颜色。为给定RGB三元组命名的最简单解决方案是使用如下列表: 您可以迭代列表(它没有那么长),并找到最接近RGB三元组的项
你在这里使用的度量可能不是很重要,但是为了细化,你可以考虑沿着强度轴的距离不那么重要(例如转换RGB到HSV,然后在计算距离之前将V除以2,这样V的距离就不那么重要了。
最好的选择,也是计算成本最高的,是使用Lab颜色空间,它更接近于感知上的一致性——那里的欧几里德距离或多或少有意义。现在你得到了最频繁的颜色值(即白色)。你想得到特定的像素吗?比如im.getpixel((im.width/2,im.height/2))?以上代码给出了最常见的颜色值如何从像素值中获取颜色值?例如。[(159、168、210)、(23、21、22)、(143、107、73)、(235、226、199)、(120、122、152)、(211、166、102)、(82、62、54)、(85、40、37)、(179、150、101)]如何表示或打印为[红色、绿色、黄色、蓝色、黑色等]