代码之家  ›  专栏  ›  技术社区  ›  betamax

使用PIL和NumPy将图像转换为Lab数组,修改值,然后再转换回来

  •  14
  • betamax  · 技术社区  · 15 年前

    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")
    

    此代码依赖于PIL、NumPy和 颜色 here colordata .txt files . 我修改了color.py,这样它就可以独立于SciPy源代码运行 似乎

    我的问题是,当我运行上面的代码时,只需将图像转换为Lab,然后再转换回RGB并保存它,我就会得到以下图像:

    alt text

    出什么问题了?是不是我在使用color.py的函数?

    供参考:
    源图像- face-him.jpg
    测试所需的所有源文件- colour-test.zip

    2 回复  |  直到 15 年前
        1
  •  10
  •   denis    15 年前


    RGB是字节0。。255,例如黄色[255255,0], rgb2xyz() 等等。在三个浮子上工作,黄色[1,1,0]。
    ( color.py lab2rgb( rgb2lab([255,255,0]) ) 是垃圾。)

    %run main.py ,然后打印数组的角点并结束?

    添加于7月13日:为了记录/为了谷歌,以下是打包、解包和转换RGB图像数组的NumPy惯用法:

        # 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()
    
        2
  •  7
  •   Nick T twasbrillig    15 年前

    lab2rgb rgb2lab ,和 RGB2实验室 似乎期望值在[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]])
    

    xyz2lab lab2xyz 函数正在做一些我不能一眼推断出来的数学运算(我对numpy或图像变换不太熟悉)。

    PIL会给你数字[0255],在传递到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)