[visit-developers] PIL tool to extract alpha channel

Meredith, Jeremy S. jsmeredith at ornl.gov
Wed Oct 1 18:03:28 EDT 2008


It could be useful, but we don't always generate images with correct
alpha, so it would need other work as well.  E.g., we probably have to
extend the volume rendering code and SR infrastructure to populate the
alpha channel.

--
Jeremy Meredith
Oak Ridge National Laboratory


> -----Original Message-----
> From: visit-developers-bounces at email.ornl.gov
[mailto:visit-developers-
> bounces at email.ornl.gov] On Behalf Of Mark Miller
> Sent: Wednesday, October 01, 2008 5:41 PM
> To: VisIt Developers
> Subject: Re: [visit-developers] PIL tool to extract alpha channel
> 
> Do we need to add a 'keep alpha' option to the Rendering Controls
> window
> , or better yet, Save Window dialog? Would that be helpful?
> On Wed, 2008-10-01 at 12:30, Sean Ahern wrote:
> > There are many times where I want to get an alpha channel from an
> image
> > I'm generating in VisIt.  This really isn't possible in many cases,
> > since we don't keep alpha around in the frame buffer.  It's
> especially
> > difficult for really transparent volume renderings.
> >
> > However, it's possible to calculate an alpha channel by rendering an
> > image twice, once with a white background and again with a black
> > background.  You can then do some math to calculate alpha and what
> the
> > original R,G,B values are.
> >
> > I've created a python script using PIL that does this work.  I've
> > attached it here.  USAGE: alpha.py whiteImage blackImage outputImage
> >
> > As an example, I've also attached some PNGs and an output PNG that
> > demonstrate the script.  I ran it as "alpha white.png black.png
> > output.png".  (Yes, I know I also changed the foreground color.
> Doesn't
> > really hurt.)
> >
> > Enjoy!
> >
> > -Sean
> >
> > __
> > Sean Ahern
> > Oak Ridge National Laboratory
> > AIM: ornlsean
> > 865-241-3748
> >
> >
> ______________________________________________________________________
> > #! /bin/sh
> > """:"
> > exec python $0 ${1+"$@"}
> > """
> >
> > from PIL import Image, ImageChops
> >
> > import sys
> >
> > if len(sys.argv) != 4:
> >     print >>sys.stderr, "Usage: %s whiteImage blackImage
> outputImage"%sys.argv[0]
> >     sys.exit(1)
> > whiteFile = sys.argv[1]
> > blackFile = sys.argv[2]
> > outFile = sys.argv[3]
> >
> > W = Image.open(whiteFile)
> > Wdata = W.getdata()
> > B = Image.open(blackFile)
> > Bdata = B.getdata()
> > alpha = ImageChops.subtract(W, B)
> > alpha = alpha.convert("L")
> > alphaData = alpha.getdata()
> > out = Image.new("RGBA", W.size)
> > outData = list(out.getdata())
> > size = W.size
> > count = 0
> > for y in range(size[1]):
> >     for x in range(size[0]):
> >         Bp = B.getpixel((x,y))
> >         a = alpha.getpixel((x,y))
> >         r = Bp[0]/255.
> >         g = Bp[1]/255.
> >         b = Bp[2]/255.
> >         a = a / 255.
> >         a = 1 - a
> >         if (a > 0.0):
> >             r = r / a
> >             g = g / a
> >             b = b / a
> >         r = int(r*256)
> >         g = int(g*256)
> >         b = int(b*256)
> >         a = int(a*256)
> >         outData[count] = (r, g, b, a)
> >         count = count + 1
> > out.putdata(outData)
> > out.save(outFile)
> --
> Mark C. Miller, Lawrence Livermore National Laboratory
> email: mailto:miller86 at llnl.gov
> (M/T/W) (925)-423-5901 (!!LLNL BUSINESS ONLY!!)
> (Th/F)  (530)-753-8511 (!!LLNL BUSINESS ONLY!!)



More information about the visit-developers mailing list