# Reconstructing opacity values in a scanned image

**URL:** <https://boards.straightdope.com/t/reconstructing-opacity-values-in-a-scanned-image/710917>\
**Category:** Factual Questions\
**Created:** [January 26, 2015, 1:20pm UTC](https://boards.straightdope.com/t/reconstructing-opacity-values-in-a-scanned-image/710917 "2015-01-26T13:20:31Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ponderoid](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/ponderoid/32/19221_2.png) [@Ponderoid](https://boards.straightdope.com/u/Ponderoid)\
**Post date:** [January 26, 2015, 1:20pm UTC](https://boards.straightdope.com/t/reconstructing-opacity-values-in-a-scanned-image/710917/1 "2015-01-26T13:20:31Z")

</div>

I’ve got several scanned images that were originally produced from transparencies that had varying opacity at different points. They weren’t just solid pictures with fully transparent backgrounds like animation cels.

I’d like an automated way to construct new PNG images with the original opacity levels intact. I have a bunch of images to do, with a few examples of each image with different flat background colors.

It seems to me someone else has probably already encountered this and solved it with software, where one can dump two or three versions of the same image with different backgrounds into some manipulation algorithm and have it spit back out a PNG or some other format with the right opacity levels in the translucent parts.

My google-fu has failed me on this, though. Everything I found when searching was how to convert the flat backgrounds into fully transparent parts, which I had already figured out on my own in 5 minutes poking around in Gimp.

---

<div class="post-metadata">

**Author:** ![Chronos](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/chronos/32/134_2.png) [@Chronos](https://boards.straightdope.com/u/Chronos)\
**Post date:** [January 26, 2015, 3:54pm UTC](https://boards.straightdope.com/t/reconstructing-opacity-values-in-a-scanned-image/710917/2 "2015-01-26T15:54:15Z")

</div>

If the images were originally produced from transparencies, then anything that was originally white should be transparent, no? And anything which is a partially-white shade should be partially transparent. I don’t think you’d need multiple backgrounds, just a single white background (which is probably how they were scanned anyway).

---

<div class="post-metadata">

**Author:** ![Ponderoid](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/ponderoid/32/19221_2.png) [@Ponderoid](https://boards.straightdope.com/u/Ponderoid)\
**Post date:** [January 26, 2015, 4:54pm UTC](https://boards.straightdope.com/t/reconstructing-opacity-values-in-a-scanned-image/710917/3 "2015-01-26T16:54:12Z")

</div>

If I had only one example, e.g. a single one scanned on a white background, I wouldn’t be able to tell if a grey pixel is grey because the original spot was opaque and grey, or was the original spot black and translucent.

By comparing two examples, say I have the same original transparency that was scanned on a green background, I could compare the two pixels between the images. If they’re the same color, that’s an opaque spot. If they’re different, that spot was translucent or transparent. They end up being different colors depending on what the background color was.

I’m pretty sure there’s some mathematical formula that could compare the RGB values of the same spot in two or maybe more examples are required and figure out what its original color and translucency values were.

---

<div class="post-metadata">

**Author:** ![Chronos](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/chronos/32/134_2.png) [@Chronos](https://boards.straightdope.com/u/Chronos)\
**Post date:** [January 26, 2015, 5:34pm UTC](https://boards.straightdope.com/t/reconstructing-opacity-values-in-a-scanned-image/710917/4 "2015-01-26T17:34:33Z")

</div>

To be clear, by “transparencies”, you mean things that were meant to be put on an overhead projector, and viewed by means of the image projected onto a screen? Because those things will not have any opaque white dye at all, since anything opaque on the transparency slide (no matter what its color) will show up as black on the screen. Likewise, they won’t have any grey, but instead partially-transparent black.

---

<div class="post-metadata">

**Author:** ![Ponderoid](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/ponderoid/32/19221_2.png) [@Ponderoid](https://boards.straightdope.com/u/Ponderoid)\
**Post date:** [January 26, 2015, 5:39pm UTC](https://boards.straightdope.com/t/reconstructing-opacity-values-in-a-scanned-image/710917/5 "2015-01-26T17:39:24Z")

</div>

I believe they were originally intended to be photographed against flat matte backgrounds, and occasionally superimposed over one another, with parts showing through. I might be using the wrong terms to describe them.

---

<div class="post-metadata">

**Author:** ![MikeS](https://avatars.discourse-cdn.com/v4/letter/m/919ad9/32.png) [@MikeS](https://boards.straightdope.com/u/MikeS)\
**Post date:** [January 26, 2015, 5:43pm UTC](https://boards.straightdope.com/t/reconstructing-opacity-values-in-a-scanned-image/710917/6 "2015-01-26T17:43:49Z")

</div>

This should be doable in principle if you have the same foreground superposed with the same opacity on two different known background images. Basically, the difference between the two final images should be proportional to the difference between the two background images; the proportionality factor for each pixel should be one minus the opacity of the foreground for each pixel. By this procedure you could construct an array of opacities for each pixel, and then use that array to reconstruct the original image.

But as to implementing this, I haven’t a clue. I could probably do it in Mathematica given an afternoon, but there’s probably better tools for it out there.

---

<div class="post-metadata">

**Author:** ![Chronos](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/chronos/32/134_2.png) [@Chronos](https://boards.straightdope.com/u/Chronos)\
**Post date:** [January 26, 2015, 6:27pm UTC](https://boards.straightdope.com/t/reconstructing-opacity-values-in-a-scanned-image/710917/7 "2015-01-26T18:27:04Z")

</div>

> [@](#):
>
> I believe they were originally intended to be photographed against flat matte backgrounds, and occasionally superimposed over one another, with parts showing through. I might be using the wrong terms to describe them.

Oh, that sounds like animation cels, then. Yeah, in that case, you could have opaque colors, and you would need multiple backgrounds to be sure. What **MikeS** said should work, though I think you might actually need three different backgrounds (or at the least, two backgrounds that are perfectly complementary-- Pure white and pure black are probably best). You’d also have to be careful about image alignment, if you’re using that technique.

> [@](#):
>
> But as to implementing this, I haven’t a clue. I could probably do it in Mathematica given an afternoon, but there’s probably better tools for it out there.

Gimp is definitely the right tool for the job, but it’s got a steep learning curve. Unless you can find a step-by-step procedure for this online, or already have enough Gimp mastery to write the step-by-step yourself, it’ll take a long time to figure it out.

---

<div class="post-metadata">

**Author:** ![Ponderoid](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/ponderoid/32/19221_2.png) [@Ponderoid](https://boards.straightdope.com/u/Ponderoid)\
**Post date:** [January 26, 2015, 7:07pm UTC](https://boards.straightdope.com/t/reconstructing-opacity-values-in-a-scanned-image/710917/8 "2015-01-26T19:07:30Z")

</div>

> [@MikeS](#):
>
> there’s probably better tools for it out there.

I hope so. You described pretty well what I’d intuitively deduced was possible, but perhaps didn’t explain all that well in my OP.

> [@Chronos](#):
>
> You’d also have to be careful about image alignment

Image alignment is perfect; I think someone before me already did that task.

> [@](#):
>
> Unless you can find a step-by-step procedure for this online,

That’s what I’m hoping to find.

It occurred to me that if I had the original physical transparency in hand, and I only had an ordinary flatbed scanner to use, I would need something like this procedure to get the equivalent PNG with a proper alpha channel for the translucent areas. So somebody somewhere perhaps has already written the software or Gimp/Photoshop steps necessary.

You’re not kidding about Gimp’s learning curve; I definitely need hand-holding to get this done.

---

<div class="post-metadata">

**Author:** ![iamthewalrus\_3](https://avatars.discourse-cdn.com/v4/letter/i/258eb7/32.png) [@iamthewalrus\_3](https://boards.straightdope.com/u/iamthewalrus_3)\
**Post date:** [January 26, 2015, 10:51pm UTC](https://boards.straightdope.com/t/reconstructing-opacity-values-in-a-scanned-image/710917/9 "2015-01-26T22:51:35Z")

</div>

Are you comfortable with command-line programs? I don’t know the exact commands, but I’m almost certain that [ImageMagick](http://www.imagemagick.org/) could do this. Possibly this [tutorial](http://www.imagemagick.org/Usage/masking/#known_bgnd) will get you going. If you put a known background pattern behind the transparency, then you can maybe use that process to find the transparency mask.

---

<div class="post-metadata">

**Author:** ![Ponderoid](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/ponderoid/32/19221_2.png) [@Ponderoid](https://boards.straightdope.com/u/Ponderoid)\
**Post date:** [January 26, 2015, 11:51pm UTC](https://boards.straightdope.com/t/reconstructing-opacity-values-in-a-scanned-image/710917/10 "2015-01-26T23:51:56Z")

</div>

Thank you, thank you, this is _exactly_ what I was looking for.

I wasn’t quite so sure when I started reading the section you linked, because it talked about removing the background and manually inserting graded translucent sections to make up for the now-missing original translucent areas, and other things that were similar, but not exactly my situation. But then I got down to [this section](http://www.imagemagick.org/Usage/masking/#two_background).

> [@](#):
>
> You really need to recover two pieces of information, how transparent each pixel in the foreground object is, and what is its original color. And you can not perfectly recover both pieces of information from just one image.
> 
> Even when you know exactly what the background image looks like, you can not just subtract it from the foreground object, unless the two are very different and known colors.
> 
> The problem is you simply can not be sure if the color that is visible is really the color given (opaque), or it is some blending of some other color and the background (semi-transparent). You can not separate the origina color from the alpha value needed, unless you have a source of some extra information.
> 
> The one situation in which you can completely recover all the details of a foreground object, is when you have two images containing two very different but completely known background colors. In that situation you do have enough information to recover both the color and its transparency of the foreground object, for a perfect background removal.

Bingo.

Thanks, everybody, now I just need to learn how to use ImageMagick®, which is thankfully free.

---

<div class="post-metadata">

**Author:** ![Senegoid](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/senegoid/32/6606_2.png) [@Senegoid](https://boards.straightdope.com/u/Senegoid)\
**Post date:** [January 27, 2015, 3:01am UTC](https://boards.straightdope.com/t/reconstructing-opacity-values-in-a-scanned-image/710917/11 "2015-01-27T03:01:50Z")

</div>

There’s also an image-manipulation program called [Curvemeister](http://curvemeister.com/) that exists largely for the purpose of fixing up image color problems. I know exactly zilch about it, except that it appears to be sophisticated. It’s not free. But the web site opens with an impressive looking slide show of color-fixed images.
