0

I'm using Pillow to cut out a shape from an image. To do this, I'm using an alpha mask that contains a black and white image of the shape I need. When I apply the mask with Pillow, I'm left with a cutout of the image, like I wanted, but it's outlined by a completely unrelated picture from a different folder.

The mask I'm using is this one: enter image description here

When I combine it with a random picture from the interwebs (this one to be exact), I get this as a result: enter image description here

The white border surrounding the image is located in a different folder from the working one and is never referenced by my code. enter image description here

I've no earthly idea how Pillow is getting the white border. It's really quite frustrating. Anybody know why this happens?

For reference, here's the code as well:

from PIL import Image

def Blend_Pic():
    background = Image.open("bg.png")
    mask = Image.open("mask2.png").convert("L")
    foreground = Image.open("fg.png")

    background.paste(foreground, (0, 0), mask)
    background.show()

if __name__ == "__main__":
    Blend_Pic()

the background is just an empty 1600x900 png, and the foreground is the random image from google.

Edit: Changing the mask to be just a simple rectangle gave the same results, so changing the mask didn't help.

watswat5
  • 671
  • 1
  • 7
  • 19
  • The answer is obvious: the code you think you're running, that you show here, is *not* the code you're actually running. Either that, or `fg.png` already contains the border from an earlier step. – Mark Ransom Nov 10 '15 at 16:50
  • I'm definitely running the right code, and the foreground picture is definitely clear of the border. I've changed the working directory several times to make sure I'm running the correct code and I re-downloaded the foreground from google, still the same problem. – watswat5 Nov 10 '15 at 17:19
  • Put a full pathname into the `open` call to make doubly sure you're getting the correct file. And put a `show` on the image just after you open it to make triply sure. – Mark Ransom Nov 10 '15 at 17:40
  • When I show the background image with pillow, it displays the mysterious border I'm coming to hate. However, the actual image itself shows as totally blank in Gimp and the windows photo viewer. I'm using an absolute address to make sure it's the same one. – watswat5 Nov 10 '15 at 17:55
  • I used the border as a base image (right size and I'm lazy) and even though I removed it from the image, it still showed up. So, I generated a new blank background and it worked. – watswat5 Nov 10 '15 at 18:00
  • There was probably some alpha component in the background image then. Since you didn't share it, it's hard to tell. – Mark Ransom Nov 10 '15 at 18:17

1 Answers1

0

After regenerating a new background image, the mysterious border went away. Despite Gimp showing nothing present, the original background image had the border hidden through some alpha trickery or something.

watswat5
  • 671
  • 1
  • 7
  • 19