Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

alpha_composite should be like paste and allow negative destinations #5298

Closed
CarlKCarlK opened this issue Mar 2, 2021 · 2 comments · Fixed by #5313
Closed

alpha_composite should be like paste and allow negative destinations #5298

CarlKCarlK opened this issue Mar 2, 2021 · 2 comments · Fixed by #5313

Comments

@CarlKCarlK
Copy link

CarlKCarlK commented Mar 2, 2021

If you try to use alpha_composite with a negative dest, it produces an error message. For example:

from PIL import Image
solid_green = Image.new("RGBA",(100,100),(0,255,0,255))
semidark = Image.new("RGBA",(100,100),(0,0,0,127))

ac_result = solid_green.copy()
ac_result.alpha_composite(semidark,(-50,50))
ac_result.show()
# => ValueError: Destination must be non-negative

Contrast this with paste, which allows negative destinations:

from PIL import Image
solid_green = Image.new("RGBA",(100,100),(0,255,0,255))
semidark = Image.new("RGBA",(100,100),(0,0,0,127))

paste_result = solid_green.copy()
paste_result.paste(semidark,(-50,50),semidark)
paste_result.show() # Works but colors aren't alpha composited

Negatives do indicate clipping on the left or top but note that alpha_composite (like paste) already allows clipping on the right and bottom.

from PIL import Image
solid_green = Image.new("RGBA",(100,100),(0,255,0,255))
semidark = Image.new("RGBA",(100,100),(0,0,0,127))

ac_result = solid_green.copy()
ac_result.alpha_composite(semidark,(50,50))
ac_result.show() # Works great!

Until this issue is addressed, here is a work around:

def alpha_composite_workaround(image0, image1, dest):
    x1,y1 = dest
    if x1 >= 0 and y1 >= 0:
        image0.alpha_composite(image1, dest=(x1, y1))
    else:
        temp = Image.new("RGBA", image0.size, (0, 0, 0, 0))
        temp.paste(image1, (x1, y1))
        image0.alpha_composite(temp)

from PIL import Image
solid_green = Image.new("RGBA",(100,100),(0,255,0,255))
semidark = Image.new("RGBA",(100,100),(0,0,0,127))

ac_result = solid_green.copy()
alpha_composite_workaround(ac_result,semidark,(-50,50))
ac_result.show()
@radarhere
Copy link
Member

alpha_composite was added in #2595 by @wiredfool. Did he have any thoughts on this?

@radarhere
Copy link
Member

I've created #5313 to resolve this.

@mergify mergify bot closed this as completed in #5313 Mar 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants