我写了一小段代码,它给出了文件夹中一组图像的色调值。我的目标是创建一个类似于本文的色调直方图:http://blog.scottlogic.com/2014/04/12/app-colour-analysis.html
我目前正在收集数据,但我只从一组+5000张图片中获得0.0s,60.0s,120.0s,180.0s,240.0s,300.0s的值。我听说色调从0到360,在文章中,看起来他得到0.0s,1.0s,2.0s,3.0s ......等等?我的代码出了什么问题?
from PIL import Image
import _imaging
import colorsys
import os
h = 0
for file in os.listdir("/path/"):
im = Image.open(file)
width, height = im.size
rgb_im = im.convert('RGB')
widthRange = range(width)
heightRange = range(height)
for i in widthRange:
for j in heightRange:
r, g, b = rgb_im.getpixel((i, j))
h, s, v = colorsys.rgb_to_hsv(r, g, b)
h = h * 360
# Rest of code stores h value for each pixel with a counter
“h”应该是每个像素的HUE值,它都存储在一个文件中。
答案 0 :(得分:1)
看起来PIL表示0到255范围内的RGB值,而colorsys期望它们在0到1的范围内。尝试在将值传递给rgb_to_hsv
之前进行分割。
r, g, b = rgb_im.getpixel((i, j))
h, s, v = colorsys.rgb_to_hsv(r/255.0, g/255.0, b/255.0)