bet*_*max 14 python numpy colors color-space python-imaging-library
我正在尝试使用NumPy将PIL图像转换为数组.然后,我想将该数组转换为Lab值,修改值,然后将数组转换回图像并保存图像.我有以下代码:
import Image, color, numpy
# Open the image file
src = Image.open("face-him.jpg")
# Attempt to ensure image is RGB
src = src.convert(mode="RGB")
# Create array of image using numpy
srcArray = numpy.asarray(src)
# Convert array from RGB into Lab
srcArray = color.rgb2lab(srcArray)
# Modify array here
# Convert array back into Lab
end = color.lab2rgb(srcArray)
# Create image from array
final = Image.fromarray(end, "RGB")
# Save
final.save("out.jpg")
Run Code Online (Sandbox Code Playgroud)
此代码取决于PIL,NumPy和颜色.颜色可以在SciPy主干中找到.我下载了color.py文件以及某些colordata .txt文件.我修改了color.py,以便它可以从SciPy的源独立运行,这一切似乎来做工精细-数组中值改变,当我运行转换.
我的问题是,当我运行上面的代码只是将图像转换为Lab,然后回到RGB并保存它我得到以下图像:

出了什么问题?这是我使用color.py函数的事实吗?
供参考:
Source Image - face-him.jpg
测试所需的所有源文件 - colour-test.zip
den*_*nis 10
在没有尝试过的情况下,缩放错误在转换颜色时很常见:
RGB是字节0 ... 255,例如黄色[255,255,0],而rgb2xyz()等等则工作在浮点数的三元组上,黄色[1.,1.,0].
(color.py没有范围检查:lab2rgb( rgb2lab([255,255,0]) )是垃圾.)
在IPython中%run main.py,然后打印srcArray的角落并结束?
添加13July:对于记录/谷歌,这里是NumPy成语打包,解包和转换RGB图像数组:
# unpack image array, 10 x 5 x 3 -> r g b --
img = np.arange( 10*5*3 ).reshape(( 10,5,3 ))
print "img.shape:", img.shape
r,g,b = img.transpose( 2,0,1 ) # 3 10 5
print "r.shape:", r.shape
# pack 10 x 5 r g b -> 10 x 5 x 3 again --
rgb = np.array(( r, g, b )).transpose( 1,2,0 ) # 10 5 3 again
print "rgb.shape:", rgb.shape
assert (rgb == img).all()
# rgb 0 .. 255 <-> float 0 .. 1 --
imgfloat = img.astype(np.float32) / 255.
img8 = (imgfloat * 255).round().astype(np.uint8)
assert (img == img8).all()
Run Code Online (Sandbox Code Playgroud)
正如丹尼斯指出的那样,没有范围检查lab2rgb或者rgb2lab,并且rgb2lab似乎期望值在[0,1]范围内.
>>> a = numpy.array([[1,2,3],[4,5,6],[7,8,9]])
>>> a
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
>>> color.lab2rgb(color.rgb2lab(a))
array([[ -1.74361805e-01, 1.39592186e-03, 1.24595808e-01],
[ 1.18478213e+00, 1.15700655e+00, 1.13767806e+00],
[ 2.62956273e+00, 2.38687422e+00, 2.21535897e+00]])
>>> from __future__ import division
>>> b = a/10
>>> b
array([[ 0.1, 0.2, 0.3],
[ 0.4, 0.5, 0.6],
[ 0.7, 0.8, 0.9]])
>>> color.lab2rgb(color.rgb2lab(a))
array([[ 0.1, 0.2, 0.3],
[ 0.4, 0.5, 0.6],
[ 0.7, 0.8, 0.9]])
Run Code Online (Sandbox Code Playgroud)
在color.py,在xyz2lab和lab2xyz功能都在做一些数学,我不能一眼推断(我没那么熟悉numpy的或图像转换).
PIL给你数字[0,255],在传递到rgb2lab函数之前尝试将它们缩小到[0,1]并在出来时备份.例如:
#from __future__ import division # (if required)
[...]
# Create array of image using numpy
srcArray = numpy.asarray(src)/255
# Convert array from RGB into Lab
srcArray = color.rgb2lab(srcArray)
# Convert array back into Lab
end = color.lab2rgb(srcArray)*255
end = end.astype(numpy.uint8)
Run Code Online (Sandbox Code Playgroud)