小编Pyl*_*lyp的帖子

将函数应用于3D numpy数组

我有一个来自Image(PIL/Pillow)对象的numpy 3D数组.

 [[178 214 235]
  [180 215 236]
  [180 215 235]
  ..., 
  [146 173 194]
  [145 172 193]
  [146 173 194]]
 ..., 
 [[126 171 203]
  [125 169 203]
  [128 171 205]
  ..., 
  [157 171 182]
  [144 167 182]
  [131 160 180]]]
Run Code Online (Sandbox Code Playgroud)

图像大小约为500x500像素.我需要为每个像素应用两个函数.

  1. 将RGB转换为LAB(使用python-colormath中的函数)此函数采用1D数组,[157, 171, 182]并返回带有LAB颜色的1D数组,例如[53.798345635, -10.358443685, 100.358443685].
  2. 使用查找自定义调色板最接近的颜色scipy.spatial.cKDTree.

自定义调色板是kd-tree.

palette = [[0,0,0], [127,127,127], [255,255,255]] #  or [[0.,0.,0.], [50.,0.,0.], [100.,0.,0.]] for LAB color
tree = scipy.spatial.cKDTree(palette)
def find nearest(pixel):
    distance, result …
Run Code Online (Sandbox Code Playgroud)

python arrays numpy image-processing multidimensional-array

2
推荐指数
1
解决办法
4998
查看次数