Re[4]: .PNG Transparency [2.5.x]

FYI.

···

------- Forwarded message -------
From: "Tim M." <terabit@gmx.de>
To: wx-users@lists.wxwidgets.org
Subject: Re: Re[4]: .PNG Transparency [2.5.x]
Date: Sat, 10 Apr 2004 15:45:59 +0200

Vadim, the great thing about PIL is that it can read virtually all image formats and automatically converts them to its own internal format that can be easily converted to a wxImage.

This is of course not of interest to the C++ users of wxWidgets, but for the wxPython users it could be very interesting to use PIL and its support for a wide variety of formats instead of being limited by wxWidgets' image handling abilities (maybe there is an equivalent lightweight open-source C++ image library?)

I modified the Python application and added a new method that allows one to load any image file with an alpha channel (not just PNGs) and that returns it as a native wxBitmap. However, unfortunately, I ran into problems, because it seems "void SetAlpha(unsigned char *alpha = NULL)" is not supported in wxPython, only "void SetAlpha(int x, int y, unsigned char alpha)" is.

If someone could add support for the first version of SetAlpha, that'd be awesome (Robin, are you listening?)

To demonstrate that the alpha channel is successfully loaded, it is set as the RGA data (wxImage::SetData works fine in wxPython).
As I mentioned, if the first version of SetAlpha was supported in wxPython, this would be a good solution for the wxPython community.

Here's the source (you have to place a PNG named "x.png" in the same folder as the .py file in order to run the example app):

import wx
import Image

class Alpha_Frame(wx.Frame):
  def __init__(self):
    wx.Frame.__init__(self, None, -1, "Image alpha blending")
    
    self.bmp = self.load_bitmap("x.png")
    self.Bind(wx.EVT_PAINT, self.on_paint)
    self.SetClientSize((self.bmp.GetWidth(), self.bmp.GetHeight()))

  def load_bitmap(self, path):
    pil = Image.open(path)
    width, height = pil.size[0], pil.size[1]

    # set rgb

    image = wx.EmptyImage(width, height)
    image.SetData(pil.convert('RGB').tostring())

    # set alpha

    r, g, b, a = pil.split()

    # image.SetAlpha(a.tostring()) # Does UNFORTUNATELY not work!
    image.SetData(a.convert('RGB').tostring())

    bitmap = wx.BitmapFromImage(image)
    return bitmap

  def on_paint(self, event):
    dc = wx.PaintDC(self)
    dc.DrawBitmap(self.bmp, (0, 0), True)

class Alpha_App(wx.App):
  def OnInit(self):
    wx.InitAllImageHandlers()
    frame = Alpha_Frame()
    frame.Show()

    return True

if __name__ == "__main__":
  app = Alpha_App(0)
  app.MainLoop()
    
Best Regards,

-Tim

On Sat, 10 Apr 2004 14:46:10 +0200 (Romance Daylight Time), Vadim > Zeitlin <zeitlin@dptmaths.ens-cachan.fr> wrote:

On Sat, 10 Apr 2004 12:57:39 +0200 "Tim M." <terabit@gmx.de> wrote:

> Vadim, wouldn't it have made more sense to use a major graphics package to
> create transparent PNGs and then implement alpha blending using these PNGs
> instead of taking one "official" PNG with an exotic, un-reproducable
> format?

I don't know which format is used by these major graphics packages, do you?
The existing code for PNG alpha support is really simple, it just reads RGBA
data and I don't know what must be done in order to support images produced by
Photoshop &c. Of course if anyone can add support for them, I'd be glad, but
I simply don't have neither time nor necessary knowledge to do it myself. In
fact, I naively assumed that libpng always provided the data as RGBA (well,
not so naively: this was the impression I had from reading its code/headers;
still I was apparently mistaken about this). If the format is more
complicated, e.g. uses palettes or what not, supporting it could be quite a
bit harder.

> I posted a really dirty alpha blending hack using PIL a week or 2 weeks
> ago and it blitted all my PNGs correctly. Alpha blending is such a nice
> feature it's a shame it does not accept ordinary transparent PNGs.

Maybe you have the required knowledge about PNG variants then. I'd be
grateful for any information about this and even more grateful for any code
implementing support for the other format versions.

Regards,
VZ

---------------------------------------------------------------------
To unsubscribe, e-mail: wx-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wx-users-help@lists.wxwidgets.org

---------------------------------------------------------------------
To unsubscribe, e-mail: wx-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wx-users-help@lists.wxwidgets.org