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 4611666
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:17:55+00:00 2026-05-22T01:17:55+00:00

There once was an image, possibly with alpha transparency, overlaid onto both a white

  • 0

There once was an image, possibly with alpha transparency, overlaid onto both a white background and black background. I have access to the two resulting images, but not the original, and I want to retrieve the original.

I have some Ruby code written up to do this, but, I think simply by nature of being in Ruby, it’s not as fast as it needs to be. Here’s the basic logic, iterating pixel by pixel:

if pixel_on_black == pixel_on_white
  # Matching pixels indicate 100% opacity in the original.
  original_pixel = pixel_on_black
elsif color_on_black == BLACK && color_on_white == WHITE
  # Black on black and white on white indicate 0% opacity in the original.
  original_pixel = TRANSPARENT
else
  # Since it's not one of the simple cases, then we'll do some math.
  # Fancy algebra tells us the following. (MAX_VALUE is the largest value
  # a channel can have. So, in most cases, 255.)

  # First, find the alpha value. This equation follows from the equations
  # for composing on black and composing on white.
  alpha = pixel_on_black.red - pixel_on_white.red + MAX_VALUE

  # Now that we know the alpha value, undo its multiplicative effect on the
  # pixel on black. By dividing. Ta da.
  alpha_ratio = alpha / MAX_VALUE
  original_pixel = Pixel.new
  original_pixel.red   = pixel_on_black.red   / alpha_ratio
  original_pixel.green = pixel_on_black.green / alpha_ratio
  original_pixel.blue  = pixel_on_black.blue  / alpha_ratio
  original_pixel.alpha = alpha
end

So that’s nice, and it works and all. However, this code needs to end up running blazing-fast, and iterating pixels in Ruby is not acceptable. It looks like, unless this function already exists somewhere, it would be in my best interest to come up with a series of ImageMagick options that would do the trick.

I’m researching ImageMagick’s command line tool now, because it looks really, really powerful, and it looks like either -fx or a series of fancy -function arguments would do the same thing as my code above. I’ll keep trying, too, but are there any ImageMagick pros out there who already know how to put all that together?

EDIT: I have an -fx version running now 🙂

convert image_on_black.png image_on_white.png -matte -channel alpha -fx "u.r + 1 - v.r" -channel RGB -fx "(u.a == 0) ? 1 : (u / u.a)" output.png

Almost an exact translation of the original code, broken into channels. Iterate over the alpha channel, and set the correct alpha values. Then iterate over the RGB channels, and dividing the channel by the alpha value (unless it’s zero, in which case we can set it to anything, since dividing by zero throws an error—in this case, I chose 1 for white).

Now time to work on converting these into more explicit arguments, since the -fx expression is reevaluated for each pixel, which isn’t great.

  • 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-22T01:17:55+00:00Added an answer on May 22, 2026 at 1:17 am

    Mkay, I surprised myself here, and I think I found an answer. It’s four ImageMagick commands, though maybe they could be worked into one somehow…though I doubt it.

    convert input_on_white.png -channel RGB -negate /tmp/convert_negative.png && \
    convert input_on_black.png /tmp/convert_negative.png -alpha Off -compose Plus -composite /tmp/convert_alpha.png && \
    composite plasma2.png /tmp/convert_alpha.png -alpha Off -channel RGB -compose Divide /tmp/convert_division.png && \
    convert /tmp/convert_division.png /tmp/convert_alpha.png -compose CopyOpacity -composite plasma_output.png
    

    (Obviously, when done, clean up those temporary files. Also, use an actual tempfile system, rather than using hardcoded paths.)

    The strategy is to first create a grayscale image that represents the alpha mask. We’ll be emulating the line of code alpha = pixel_on_black.red - pixel_on_white.red + MAX_VALUE, which can be rewritten as alpha = pixel_on_black.red + (MAX_VALUE - pixel_on_white.red).

    So, line 1: we create an image that represents the second term of that equation, a negated version of the RGB channels of image-on-white. We save it as a temporary file.

    Then, line 2: we want to add that negative image to the image-on-black. Use ImageMagick’s Plus composition, and save that as the temporary alpha mask. The result is a grayscale image where white represents areas that should have 100% opacity in the final image, and black represents areas that will later be fully transparent.

    Then, line 3: bring the image-on-black back to the original RGB colors. Since the image-on-black was created by mutiplying the RGB channels by the alpha ratio, we divide by the alpha mask image to undo that effect.

    Finally, line 4: take the color-corrected image from line 3 and apply the alpha mask from line 2, using ImageMagick’s CopyOpacity composition function. Ta da!

    My original strategy took anywhere from 5-10 seconds. This strategy takes less than a second. Much, much, much better.

    Unsurprisingly, asking for help is what drove me to find the answer myself. Regardless, I’ll leave this question open for 48 hours to see if anyone finds a slightly more optimal solution. Thanks!

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

Sidebar

Related Questions

I remember reading once that there was a way to embed an image into
I have a web page that uses a large image for its background. I
Someone told me once there was a good novel around the story of the
Once again one of those: Is there an easier built-in way of doing things
Is there any clever method out there to make my executeEveryDayMethod() execute once a
Is there an equivalent to browser cookies for Smart Client / Click Once application
Once again I'm doing Java graphics (Graphics2D) but I noticed there is no Polygon.Double
Once it is compiled, is there a difference between: delegate { x = 0;
Is there a way to automatically publish a website to multiple locations at once?
Question Is there a way to define a method only once in C# (in

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.