Using PIL and NumPy to convert an image to Lab array, modify the values and then convert back
color-space, colors, numpy, python, python-imaging-library
Solution
Without having tried it, scaling errors are common in converting colors: RGB is bytes 0 .. 255, e.g. yellow [255,255,0], whereas `rgb2xyz()` etc. work on triples of floats, yellow [1.,1.,0]. (`color.py` has no range checks: `lab2rgb( rgb2lab([255,255,0]) )` is junk.)
In IPython, `%run main.py`, then print corners of srcArray and end ?
Added 13July: for the record / for google, here are NumPy idioms to pack, unpack and convert RGB image arrays:
# 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()
Problem
I am trying to convert a PIL image into an array using NumPy. I then want to convert that array into Lab values, modify the values and then convert the array back in to an image and save the image. I have the following code: ``` 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") ``` This code is dependent on PIL, NumPy and color. color can be found in the SciPy trunk here. I downloaded the color.py file along with certain colordata .txt files. I modified the color.py so that it can run independently from the SciPy source and it all seems to work fine - values in the array are changed when I run conversions. My problem is that when I run the above code which simply converts an image to Lab, then back to RGB and saves it I get the following image back: What is going wrong? Is it the fact I am using the functions from color.py? For reference: Source Image - face-him.jpg All source files required to test - colour-test.zip