0

I'm trying to erase the black background from image with pillow:

image

I have this code:

from PIL import Image, ImageOps, ImageDraw
size = (200, 200)
mask = Image.new('L', size, 0)
draw = ImageDraw.Draw(mask) 
draw.ellipse((0, 0) + size, fill=255)

im = Image.open('image.png')

output = ImageOps.fit(im, mask.size, centering=(0.5, 0.5))
output.putalpha(mask)

output.save('output.png')

Why doesn't this work? How can I fix it?

eyllanesc
  • 221,139
  • 17
  • 121
  • 189
  • Erase to what? Erase to white? Erase to transparent? – Tim Roberts Dec 14 '21 at 04:35
  • Erase to transparent – Peliculas Y Series Dec 14 '21 at 04:35
  • I tried putting `pillow remove background` into a search engine and got multiple tutorials right away. Did they work for you? Why not? – Karl Knechtel Dec 14 '21 at 04:38
  • You said "why doesn't this work", but it clearly DOES work. You have drawn a circular mask, with alpha=255 inside and alpha=0 outside, applied it to your image, and the result is exactly that. You haven't tried selecting the black pixels here, so I assume you weren't expecting that to work. – Tim Roberts Dec 14 '21 at 04:39
  • It can be easier to do that kind of manipulation in `numpy`. Read this: https://stackoverflow.com/questions/3752476/python-pil-replace-a-single-rgba-color – Tim Roberts Dec 14 '21 at 04:40

0 Answers0