I'm writing a small wxPython application that performs a hue rotation on all graphics and CSS files in a website template.  It successfully displays and saves PNG files with transparency, but cannot save GIF files with transparency from wxPython, so I'm trying to save a transparent GIF from PIL, so far without success.  The hue rotation works, the code saves the GIF in a file, but the GIF's pixels that should be transparent come out black.

This is a PIL question because I'm converting a wx.Image to a PIL.Image and using PIL to save the GIF.  I've read and tested nearly everything
about this question I can turn up on Google and my code still doesn't work.  Could someone here recommend a fix to my attached working sample code (probably to line 39 or 41)?  Thank you!

Tim Black

GIF image

#Boa:Frame:Frame1

import wx
import PIL
from PIL import Image

def create(parent):
    return Frame1(parent)

[wxID_FRAME1] = [wx.NewId() for _init_ctrls in range(1)]

class Frame1(wx.Frame):
    def _init_ctrls(self, prnt):
        wx.Frame.__init__(self, style=wx.DEFAULT_FRAME_STYLE, name='', 
parent=prnt, title='Frame1', pos=wx.Point(-1, -1), id=wxID_FRAME1, 
size=wx.Size(-1, -1))

    def __init__(self, parent):
        self._init_ctrls(parent)
        wxim = wx.Image('logo.gif')
        bmp = wxim.ConvertToBitmap() # emulates necessary conversion in larger 
program
        wxim = bmp.ConvertToImage()
##        wxim.ConvertAlphaToMask()   # allows transparency to display
##        wxim.InitAlpha()            # later in wx.BitmapButton
        wxim.RotateHue(0.4)
        imageData = wxim.GetData()
##        print imageData
        if wxim.HasAlpha(): # GIF files don't create an alpha channel, so this 
section is for reference
            print 'has alpha'
            alpha = wxim.GetAlphaData()
            i = 0
            newImageData = ''
            for alphaVal in alpha: # map alpha values into RGB imageData to 
create RGBA newImageData
                i = i + 3
                newImageData = newImageData + str(imageData[i-3:i]) + 
str(alphaVal)
            imagePIL = PIL.Image.fromstring('RGBA', (wxim.GetWidth(), 
wxim.GetHeight()), newImageData)
            imagePIL = imagePIL.convert('RGB') # because PIL.Image can't 
convert RGBA -> P
        else:
            imagePIL = PIL.Image.fromstring('RGB', (wxim.GetWidth(), 
wxim.GetHeight()), imageData)
        imagePIL = imagePIL.convert("P", dither=Image.NONE, 
palette=Image.ADAPTIVE) # prevent dithering
        imagePIL.info["transparency"] = 0
##        print imagePIL.info, imagePIL.mode
        imagePIL.save('logo2.gif', transparency=0)
        

if __name__ == '__main__':
    app = wx.PySimpleApp()
    wx.InitAllImageHandlers()
    frame = create(None)
##    frame.Show()
##
##    app.MainLoop()
_______________________________________________
Image-SIG maillist  -  Image-SIG@python.org
http://mail.python.org/mailman/listinfo/image-sig

Reply via email to