【发布时间】:2021-05-26 23:45:15
【问题描述】:
我正在尝试使用我不熟悉的 NumPy 按亮度对图像进行排序。我设法创建了一个随机图像并对其进行排序。
def create_image(output, width, height, arr):
array = np.zeros([height, width, 3], dtype=np.uint8)
numOfSwatches = len(arr)
swatchWidth = int(width/ numOfSwatches)
for i in range (0, numOfSwatches):
m = i * swatchWidth
r = (i+1) * swatchWidth
array[:, m:r] = arr[i]
img = Image.fromarray(array)
img.save(output)
到目前为止一切顺利。只是现在我想从创建随机图像切换到加载它们并然后对它们进行排序。
#!/usr/bin/python3
import numpy as np
from PIL import Image
# --------------------------------------------------------------
def load_image( infilename ) :
img = Image.open( infilename )
img.load()
data = np.asarray( img, dtype = "int32" )
return data
# --------------------------------------------------------------
def lum (r,g,b):
return math.sqrt( .241 * r + .691 * g + .068 * b )
myImageFile = "random_colours.png"
imageNP = load_image(myImageFile)
imageNP.sort(key=lambda rgb: lum(*rgb) )
我得到的错误是 TypeError: 'key' is an invalid keyword argument for this function 我可能错误地创建了 NP 数组,因为它是一个随机 NP 数组。
【问题讨论】:
标签: python numpy colors python-imaging-library