Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 971577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:59:11+00:00 2026-05-16T02:59:11+00:00

I have two images, both with alpha channels. I want to put one image

  • 0

I have two images, both with alpha channels. I want to put one image over the other, resulting in a new image with an alpha channel, just as would occur if they were rendered in layers. I would like to do this with the Python Imaging Library, but recommendations in other systems would be fantastic, even the raw math would be a boon; I could use NumPy.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T02:59:11+00:00Added an answer on May 16, 2026 at 2:59 am

    I couldn’t find an alpha composite function in PIL, so here is my attempt at implementing it with numpy:

    import numpy as np
    from PIL import Image
    
    def alpha_composite(src, dst):
        '''
        Return the alpha composite of src and dst.
    
        Parameters:
        src -- PIL RGBA Image object
        dst -- PIL RGBA Image object
    
        The algorithm comes from http://en.wikipedia.org/wiki/Alpha_compositing
        '''
        # http://stackoverflow.com/a/3375291/190597
        # http://stackoverflow.com/a/9166671/190597
        src = np.asarray(src)
        dst = np.asarray(dst)
        out = np.empty(src.shape, dtype = 'float')
        alpha = np.index_exp[:, :, 3:]
        rgb = np.index_exp[:, :, :3]
        src_a = src[alpha]/255.0
        dst_a = dst[alpha]/255.0
        out[alpha] = src_a+dst_a*(1-src_a)
        old_setting = np.seterr(invalid = 'ignore')
        out[rgb] = (src[rgb]*src_a + dst[rgb]*dst_a*(1-src_a))/out[alpha]
        np.seterr(**old_setting)    
        out[alpha] *= 255
        np.clip(out,0,255)
        # astype('uint8') maps np.nan (and np.inf) to 0
        out = out.astype('uint8')
        out = Image.fromarray(out, 'RGBA')
        return out
    

    For example given these two images,

    img1 = Image.new('RGBA', size = (100, 100), color = (255, 0, 0, 255))
    draw = ImageDraw.Draw(img1)
    draw.rectangle((33, 0, 66, 100), fill = (255, 0, 0, 128))
    draw.rectangle((67, 0, 100, 100), fill = (255, 0, 0, 0))
    img1.save('/tmp/img1.png')
    

    enter image description here

    img2 = Image.new('RGBA', size = (100, 100), color = (0, 255, 0, 255))
    draw = ImageDraw.Draw(img2)
    draw.rectangle((0, 33, 100, 66), fill = (0, 255, 0, 128))
    draw.rectangle((0, 67, 100, 100), fill = (0, 255, 0, 0))
    img2.save('/tmp/img2.png')
    

    enter image description here

    alpha_composite produces:

    img3 = alpha_composite(img1, img2)
    img3.save('/tmp/img3.png')
    

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two images next to each other in HTML. One image is taller
I have two images in my drawable folder, one called Red and the other
I have two images that need to be overlaid over one another, and they
In my application i have two images original upper image and down reflection one.
I have two images that I want adjacent but they appear one above the
I have two divs both containing images. Here is some example code: <div id=one>
I have two images, both taken at the same time from the same detector.
I have two images (JPGs). Both are the same width but image2 is shorter
I have two images - 1)a rectangular one, displaying the actual content and 2)a
I have two images. One that is normal, and another that is more colourised.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.