0

I have a shape below:

and I would like to colour only this shape, the background is transparent so I feel as if there is some way to colour an entire image. Right now I am using put pixel and colouring every pixel in the picture but I am wondering if there is a more efficient way to do this?

putpixel(xy=(i,j), value=(red)), where i,j is coloured if it exists

rabiaasif
  • 305
  • 3
  • 6
  • 21
  • Using this answer [Python: PIL replace a single RGBA color](https://stackoverflow.com/questions/3752476/python-pil-replace-a-single-rgba-color) I was able to do it by changing 2 lines, the color the values in `grey_area = (red == 138) & (blue == 138) & (green == 138)` and `data[..., :-1][grey_area.T] = (255, 0, 0) # Transpose back needed` It produced this image https://i.imgur.com/OsqwhV6.png. I think it should also preserve the transparent background. The answer suggests to use "numpy, it provides a much, much faster way to operate on PIL images." – chickity china chinese chicken Nov 27 '18 at 18:57
  • this was great it works perfectly, however it does not let me change the opacity of the colour by adding in an alpha value... Do you know what I would need to do to be able to change the opacity of the image – rabiaasif Nov 28 '18 at 20:23
  • yes, if you define an opacity level of, say, `opacity_level = 50` then you could change the line to `data[..., :][grey_area.T] = (255, 0, 0, opacity_level)`. Is that what you are asking? – chickity china chinese chicken Nov 28 '18 at 21:34
  • I had to change a little more than that! I will post a solution below... – rabiaasif Nov 29 '18 at 15:08

1 Answers1

0

Solution modified from: fill one colour

Where these two lines were changed:

white_areas = (red == 138) & (blue == 138) & (green == 138) & (alpha == 255)
data[..., ][white_areas.T] = (25, 90, 0, 190)

previous solution:

white_areas = (red == 255) & (blue == 255) & (green == 255)
data[..., :-1][white_areas.T] = (255, 0, 0) # Transpose back needed

Removed :-1 from data

What these changes mean: also able to change opacity level

rabiaasif
  • 305
  • 3
  • 6
  • 21