[Image-SIG] Removing specific range of colors from scanned image

Eduardo Ismael eismb at hotmail.com
Tue Apr 28 05:31:02 CEST 2009


Thank you very much for your feedback, John. It works wonders!!!

Would you please make comments on what I could do to improve the code below?

im = Image.open("C:\Scan_119.jpg")
pixels = im.load()
width, height = im.size

def distance (a, b):
    return ((a[0]- b[0])** 2 + (a[1]- b[1])** 2 + (a[-1]- b[-1]) ** 2) ** 0.5

blue = (205, 241, 255) # a value I am presuming would be the average blue in the image

for x in range(width):
    for y in range(height):
        if distance(pixels[x, y], blue) < 50:
            pixels[x, y] = (255, 255, 255)

I guess I got the main idea, but please tell me if there is anything I could do to improve it. Also, I don't think I followed your advice on "processing at the image level rather than by looping over pixels" Would you correct it for me?

By the way, thanks to your explanation of the color cube approach, I was able to come up with this other script:

im = Image.open("C:\Scan_119.jpg")
pixels = im.load()
width, height = im.size
for x in range(width):
        for y in range(height):
            if pixels[x, y][0] <= 210: # Red above 210 tended not to create blue
                # Green could be ignored in this case, i guess
                if pixels[x, y][-1] >= 170: # Blue under 170 seemed to be around the image's blue
                    pixels[x, y] = (255, 255, 255)

which affected what I would call purple a bit, but it was quite precise otherwise. Well, I guess it just needs some adjusting. I came up with R210 and B170 after looking at a color cube I adapted. 

What method you suggest I should stick to - in other words, which one seems to bring more accuracy?

Also, If not RGB, which color space would be the best?


> From: jcupitt at gmail.com
> Date: Mon, 27 Apr 2009 11:56:00 +0100
> Subject: Re: [Image-SIG] Removing specific range of colors from scanned image
> To: eismb at hotmail.com
> CC: image-sig at python.org
> 
> 2009/4/25 Eduardo Ismael <eismb at hotmail.com>:
> > I scanned a color page from a book and I would like to remove specific
> > colors from it.
> 
> The best solution is to calculate a colour difference metric. This is
> rather like the 'magic wand' in most paint programs.
> 
> RGB isn't the best colour space for this, but it would sort-of work.
> The idea is: imagine that your three colour values are coordinates in
> a cube. You want to find pixels whose value is close to the position
> of a point in the blue corner of this cube, in other words, something
> like:
> 
>    for pixel in image:
>      if distance (image[pixel], blue) < 10:
>        image[pixel] = white
> 
> The simplest distance function is just pythagoras, ie.
> 
>   def distance (a, b):
>     vector = a - b
>     return (vector.red ** 2 + vector.green ** 2 + vector.blue ** 2) ** 0.5
> 
> You can do this efficiently by processing at the image level rather
> than by looping over pixels (always incredibly slow). So: calculate an
> image where each pixel is the distance function for that point, then
> threshold and use that mask to set sections of your original image to
> white.
> 
> John

_________________________________________________________________
Novo Windows Live: Messenger 2009 e muito mais. Descubra!
http://www.windowslive.com.br
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/image-sig/attachments/20090428/cdbab7ad/attachment-0001.htm>


More information about the Image-SIG mailing list