Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing PIL and NumPy to convert an image to Lab array, modify the values and then convert back
    text
    copied!<p>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:</p> <pre><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") </code></pre> <p>This code is dependent on PIL, NumPy and <em>color</em>. color can be found in the SciPy trunk <a href="http://svn.scipy.org/svn/scipy/tags/pre_org/Lib/image/color.py" rel="noreferrer">here</a>. I downloaded the color.py file along with certain <a href="http://svn.scipy.org/svn/scipy/tags/pre_org/Lib/image/" rel="noreferrer">colordata .txt files</a>. I modified the color.py so that it can run independently from the SciPy source and it all <em>seems</em> to work fine - values in the array are changed when I run conversions.</p> <p>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:</p> <p><img src="https://imgur.com/kI2WY.jpg" alt="alt text"></p> <p>What is going wrong? Is it the fact I am using the functions from color.py?</p> <p>For reference:<br> Source Image - <a href="https://imgur.com/81SSp.jpg" rel="noreferrer">face-him.jpg</a><br> All source files required to test - <a href="http://www.maxnov.com/colour-test.zip" rel="noreferrer">colour-test.zip</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload