1

I have an image that I'm attempting to edit, using the PIL Python image library. I'm attempting to make the light-tan background transparent, by replacing pixels with a certain RGBA code with a transparent pixel. I've been looking at Python: PIL replace a single RGBA color, and have edited it to allow a buffer to pick up pixels which aren't exactly the same colour. This is my current code:

data = numpy.array(im)
red, green, blue, alpha = data.T

for r in range(245, 256):
    for g in range(245, 256):
        for b in range(210, 230):
            white_areas = (red == r) & (green == g) & (blue == b) & (alpha == 255)
            if len(data[...][white_areas.T]) != 0:
                data[...][white_areas.T] = (0, 0, 0, 0)

I've found, however, that around the image, there's still a few pixels that hang around because of some colour blur, and also some around the outside edge of the image border. This is clearly visible when added to a dark background. I find that when I increase the buffer for the RGB codes, the pixel blur becomes less, except if I increase it too much, the white in the image often gets picked up accidentally.

What would possibly be a way to remove the blur (like a paint bucket tool with high tolerance), while at the same time not increasing the time taken?

Rocked 03
  • 13
  • 4

0 Answers0