我找到了这个 RGB 图像分析并尝试在一些随机图片上使用它。
http://marksolters.com/programming/2015/02/27/rgb-histograph.html
import numpy as np
import mpl_toolkits.mplot3d.axes3d as p3
import matplotlib.pyplot as plt
import colorsys
from PIL import Image
# (1) Import the file to be analyzed!
img_file = Image.open("another_photo.jpeg")
img = img_file.load()
# (2) Get image width & height in pixels
[xs, ys] = img_file.size
max_intensity = 100
hues = {}
# (3) Examine each pixel in the image file
for x in range(0, xs):
for y in range(0, ys):
# (4) Get the RGB color of the pixel
[r, g, b] = img[x, y]
# (5) Normalize pixel color values
r /= 255.0
g /= 255.0
b /= 255.0
# (6) Convert RGB color to HSV
[h, s, v] = colorsys.rgb_to_hsv(r, g, b)
# (7) Marginalize s; count how many pixels have matching (h, v)
if h not in hues:
hues[h] = {}
if v not in hues[h]:
hues[h][v] = 1
else:
if hues[h][v] < max_intensity:
hues[h][v] += 1
# (8) Decompose the hues object into a set of one dimensional arrays we can use with matplotlib
h_ = []
v_ = []
i = []
colours = []
for h in hues:
for v in hues[h]:
h_.append(h)
v_.append(v)
i.append(hues[h][v])
[r, g, b] = colorsys.hsv_to_rgb(h, 1, v)
colours.append([r, g, b])
# (9) Plot the graph!
fig = plt.figure()
ax = p3.Axes3D(fig)
ax.scatter(h_, v_, i, s=5, c=colours, lw=0)
ax.set_xlabel('Hue')
ax.set_ylabel('Value')
ax.set_zlabel('Intensity')
fig.add_axes(ax)
plt.show()
但是我的代码在第 20 行中断:
[r, g, b] = img[x, y]
有人可以向我解释一下作者如何以及为什么从 img
中解压这两个值并将它们分配给三个变量吗?
最佳答案
这取决于图像。你的可能是 RGBA,然后像素元组有 4 个值,最后一个是 ALPHA。您可以使用 .convert('RGB') 如下:
im = Image.open(file_name).convert('RGB')
width, height = im.size
pixels = im.load()
# this list comprehension will get you the list of all pixels in RGB format
all_pixels = [pixels[x,y] for y in range(height) for x in range(width)]
关于python - 为什么我收到 ValueError : too many values to unpack (expected 3)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49515887/