Home > Mobile >  Why is there a black background of a file with a transparent png posted on top of a regular pdf?
Why is there a black background of a file with a transparent png posted on top of a regular pdf?

Time:02-03

I'm pasting a transparent png that should only take up the bottom of the new image when it is pasted on top of a picture; however, when I paste it on top of the image, it shows the pasted png part, and then the rest above it is all black. I was wondering how I could fix it and if it has something to do with the masking of the pasted png?

import sys
from PIL import Image, ImageOps

def main():
    def paster(inp, output):
        shirt = Image.open('shirt.png')
        initial = Image.open(inp)
        #making size variable to use to resize shirt png before pasting it
        size = initial.size
        p = ImageOps.fit(shirt, size, method=Image.BICUBIC, bleed=0.0, centering=(0.5, 0.5))
        #pasting shirt 'p' over initial image
        initial.paste(p)
        initial.save(output)


    if len(sys.argv) < 3:
        sys.exit('Too few command-line arguments')
    elif len(sys.argv) > 3:
        sys.exit('Too many command-line arguments')
    else:
       paster(sys.argv[1], sys.argv[2])


if __name__ == '__main__':
    main()

CodePudding user response:

Two issues:

  1. You are resizing the wrong image in ImageOps.fit() (and setting way too many unneeded parameters). Resize initial to be the same size as shirt, and use the default values (don't add keyword parameters).
  2. Define the mask= parameter when you paste.
  • Related