[SciPy-User] Image Enhancing in Python

Tom Dimiduk tdimiduk@physics.harvard....
Fri Oct 7 10:28:08 CDT 2011

That shouldn't be too hard.  When I work with images I use PIL and 
scipy.  If you have those two libraries installed, this code should do 
what I think you want (if I am interpreting what you want correctly).  I 
have not actually tested this code, so there may be a few bugs, but it 
should get you the right idea.


# this loads the image as an NxMx3 array with the color chanels in the
# third direction
import numpy as np
import Image
from scipy.misc.pilutil import fromimage
im = Image.open(filename)
im = fromimage(im).astype('d')

# and here we do the channel arithmetic you like
red = im[...,0]
gray = im[...,1] + im[...,2]
# red and gray are now NxM images which have the data you want

# This is now two images, if you want it to be 1 you could use vstack
# to put them back together into a single image, I would suggest then
# putting the "gray" value in twice so that it is an NxMx3 image again
# as things will expect.
composite = np.vstack((red, gray, gray))

On 10/07/2011 06:46 AM, Lorenzo Isella wrote:
> Dear All,
> I hope this is not too off topic.
> I saw some wikis about Python and image analysis. What I am trying to
> do should not be too difficult, but I am unsure about how to proceed.
> It is my understanding that a color image (RGB), for Python (and not
> only) is nothing else that a combination of 3 different 2D arrays.
> What I would like to do is the following: take this color image, turn
> it into a grayscale image (and this is the easy bit) with the
> exception of the red color (which should be preserved).
> The final image should therefore be grayscale and red. I suppose I
> will have to do some thresholding by hand to select which red shades I
> want to keep, but I am unsure about how to proceed.
> Any suggestion is welcome.
> Cheers
> Lorenzo
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user

More information about the SciPy-User mailing list