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: jcup...@gmail.com
> Date: Mon, 27 Apr 2009 11:56:00 +0100
> Subject: Re: [Image-SIG] Removing specific range of colors from scanned image
> To: ei...@hotmail.com
> CC: image-sig@python.org
>
> 2009/4/25 Eduardo Ismael <ei...@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
_______________________________________________
Image-SIG maillist - Image-SIG@python.org
http://mail.python.org/mailman/listinfo/image-sig