wxImage from PNG with an alpha channel (try #2 with code)

Apparently, the wxImage.HasAlpha() function is lying to me most of the
time. That is, only for some PNG files with transparency. Also, method
wxImage.GetOrFindMaskColour() returns a 3-tuple, even for files that do
read correctly ! This is baffling since alpha values singular in PNG
RGBA files. When reading does work, it works well. It’s a go/no-go
situation which seems semi-random depending on the particular file.

` self.wxImage = wx.Image( ‘TRY.PNG’, wx.BITMAP_TYPE_PNG )

    hasAlpha = self.wxImage.HasAlpha()

`

File
“C:\PROGRA~1\Python25\lib\site-packages\wx-2.8-msw-unicode\wx_core.py”,

line 2829, in GetAlpha

return core.Image_GetAlpha(*args, **kwargs)

wx._core.PyAssertionError: C++ assertion “HasAlpha()” failed at
…\src\common

\image.cpp(1643) in wxImage::GetAlpha(): no alpha channel

Have I smacked into a bug, or perhaps a known shortcoming ? Is there
another way to get images to always handle detecting the alpha channel
properly ?

I have attached 2 small images and the code file.

Platform Windows 6.1.7600

Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit
(Intel)]

Python wx 2.8.10.1

WIN7 64-bit

Z_IMAGE_MANIP.PY (3.95 KB)

TRY.PNG

SMALL_PIC.PNG

···


To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

In wx there is a difference between masks and alpha channels, while in PNG files a mask is just a special case of an alpha channel, so this can easily be the source of confusion sometimes. When a PNG file is loaded by wx.Image then if all pixels are either fully transparent or fully opaque, then the image is assigned a mask colour and when it is converted to a wx.Bitmap it will use a 24-bit format (usually, depending on platform) and a wx.Mask object will be assigned to it. If the PNG has any pixel with a transparency value between 0 and 255 then it is given an alpha channel instead.

This dichotomy between masks and alpha exists because wx has been around longer than PNG has existed, and using a mask colour is how transparency was (and still is) handled in earlier image file formats that don't deal with alpha channels.

GetOrFindMaskColour returning only the RGB values is correct, since if you are interested in the mask it is assumed that you are not interested in the alpha channel, and because in wx.Image a mask is defined by pixels that have an otherwise unused colour value.

···

On 5/14/10 2:01 PM, Ray Pasco wrote:

Apparently, the wxImage.HasAlpha() function is lying to me most of the
time. That is, only for some PNG files with transparency. Also, method
wxImage.GetOrFindMaskColour() returns a 3-tuple, even for files that do
read correctly ! This is baffling since alpha values singular in PNG
RGBA files. When reading does work, it works well. It's a go/no-go
situation which seems semi-random depending on the particular file.

self.wxImage = wx.Image( 'TRY.PNG', wx.BITMAP_TYPE_PNG )
hasAlpha = self.wxImage.HasAlpha()

File
"C:\PROGRA~1\Python25\lib\site-packages\wx-2.8-msw-unicode\wx\_core.py",
line 2829, in GetAlpha
return _core_.Image_GetAlpha(*args, **kwargs)
wx._core.PyAssertionError: C++ assertion "HasAlpha()" failed at
..\..\src\common
\image.cpp(1643) in wxImage::GetAlpha(): no alpha channel

Have I smacked into a bug, or perhaps a known shortcoming ? Is there
another way to get images to always handle detecting the alpha channel
properly ?

--
Robin Dunn
Software Craftsman

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

"since if you are interested in the mask it is assumed that you are
not interested

in the alpha channel, and because in wx.Image a mask is defined by
pixels that have an otherwise unused colour value."

This sounds like the wx.Image mask was designed *only* for .GIF-type
files that have a limited palette of colors, with one "color" value
that is assigned to be the 100% transparency flag.

Yes, that was the era in which wxImage was created, but it has grown since then too.

Then, that leaves me the questions:

-) How is variable (0..255) transparency (alpha) implemented in wx ?

I didn't say that wxImage can't do alpha, just that if all the pixels in the loaded image are all either fully transparent or fully opaque then a mask will be used instead. It is functionally equivalent.

Because of its heritage wxImage does store the alpha in a separate array than the RGB bytes, but for most things the API hides that enough that you shouldn't need to care about it.

-) It it better or even mandatory to use another package such as Pil
to manipulate RGBA images to maintain alpha transparency ?, e.g.:

You can, but for many things it is not necessary.

     manipulatePilImageReadByPilFromPngFile( pilImage )
     if (pilImage.mode == 'RGBA') :
         wxImage = wx.EmptyImage( *pilImage.size )
         wxImage.SetData( pilImage.convert( 'RGB' ).tostring() )
         wxImage.SetAlphaData( pilImage.convert( 'RGBA' ).tostring()
[3::4] )

     dc.DrawBitmap( wxImage.ConvertToBitmap(), offX, offY )

-) How does wx determine whether to use (100% transparency) values on
a PNG file that has does have an alpha plane ? ("RGBA" images, in PIL-
speak) The functions HasAlpha() and wxImage.GetAlphaData() fail on
*some*, but *only* some, perfectly valid RGBA type PNG files.

If any pixel is partially transparent then an alpha channel is used. Otherwise it is treated as a mask.

···

On 5/16/10 9:08 AM, Ray Pasco wrote:

--
Robin Dunn
Software Craftsman

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

See http://wiki.wxpython.org/index.cgi/WorkingWithImages

Also download the wxPython Demo. In the "Using Images" section, there
is an Alpha Drawing demo and an ImageAlpha demo. There's probably some
others there too.

···

On May 18, 9:07 am, Ray Pasco <raoulpa...@yahoo.com> wrote:

Thanks, but I still haven't found examples or a tutorial to show how
alpha is handled and manipulated using wx.

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

Ray Pasco wrote:

I had to rewrite the conversion routines found on
PyWiki: 'Working With Images' @ http://wiki.wxpython.org/index.cgi/WorkingWithImages
The routines now handle alpha planes under all circumstances.

Have you updated the Wiki -- this sounds useful.

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

It's actually pretty easy, but you're welcome to just write the text
up and send it to me. I can update the wiki for you...

···

On May 20, 9:24 pm, Ray Pasco <raoulpa...@yahoo.com> wrote:

The procedure to make and update the wiki looks daunting to me.
Perhaps I'm just not familiar with formatting wiki pages. There's so
many text-based formatting commands.
.P
I'm getting flashbacks to Wordstar days.
.E

I'll have to look into this some more.

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

For the archives, Ray gave me his code and PNG image, so I put them up
on the wiki here:

http://wiki.wxpython.org/PngWithAlphaChannel

I put it in the Wiki's cookbook with a link here:
http://wiki.wxpython.org/RecipesImagesAndGraphics

···

On May 21, 11:16 am, Mike Driscoll <kyoso...@gmail.com> wrote:

On May 20, 9:24 pm, Ray Pasco <raoulpa...@yahoo.com> wrote:

> The procedure to make and update the wiki looks daunting to me.
> Perhaps I'm just not familiar with formatting wiki pages. There's so
> many text-based formatting commands.
> .P
> I'm getting flashbacks to Wordstar days.
> .E

> I'll have to look into this some more.

It's actually pretty easy, but you're welcome to just write the text
up and send it to me. I can update the wiki for you...

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en