Saving a screenshot of a windows' contents as GIF not working

I have the following event handler code:

    def on_export(self, event):
        """
        Exports the current tab as an image.
        """
        wc = "PNG (*.png)|*.png|JPEG (*.jpg, *.jpeg)|*.jpeg;*.jpg|GIF (*.gif)|*.gif|BMP (*.bmp)|*.bmp|TIFF (*.tiff)|*.tiff"
        dlg = wx.FileDialog(self, "Export data to...", os.getcwd(),
                style=wx.SAVE | wx.OVERWRITE_PROMPT, wildcard=wc)
        if dlg.ShowModal() == wx.ID_OK:
            filename = dlg.GetPath()
            _name = os.path.splitext(filename)[1].replace(".", "")

            types = {0: "png", 1: "jpg", 2: "gif", 3: "bmp", 4: "tiff"}

            if not os.path.splitext(filename)[1]:
                _name = types[dlg.GetFilterIndex()]
                filename += "." + _name
            if not _name in self.util.types[2:]:
                wx.MessageBox("Invalid filetype to export as.", "Invalid type")
            else:
                self.board.export(filename)
        dlg.Destroy()

then, in board.export (pretty much taken from the wiki cookbook):

    def export(self, filename):
        """
        Exports the current view as a file. Select the appropriate wx constant
        depending on the filetype.
        """
        _name = os.path.splitext(filename)[1].replace(".", "").lower()

        types = {"png": wx.BITMAP_TYPE_PNG, "jpg": wx.BITMAP_TYPE_JPEG, "jpeg":
                 wx.BITMAP_TYPE_JPEG, "bmp": wx.BITMAP_TYPE_BMP, "gif":
                 wx.BITMAP_TYPE_GIF, "tiff": wx.BITMAP_TYPE_TIF, "pcx":
                 wx.BITMAP_TYPE_PCX }

        const = types[_name]

        context = wx.BufferedDC(None, self.buffer, wx.BUFFER_VIRTUAL_AREA)
        memory = wx.MemoryDC()
        x, y = self.GetClientSizeTuple()
        bitmap = wx.EmptyBitmap(x, y, -1)
        memory.SelectObject(bitmap)
        memory.Blit(0, 0, x, y, context, 0, 0)
        memory.SelectObject(wx.NullBitmap)
        bitmap.SaveFile(filename, const)

It works great for every file type except for gifs - it creates a 0-byte image. All the other file formats work correctly, so I'm not sure what's wrong. I'm using 2.8.9.1, python 2.5.2, Ubuntu 8.10 64bit, kernel 2.6.27-9-generic #1 SMP

I don’t think there is something wrong with your code.
a simple
wx.Bitmap(“test.png”).SaveFile(“test.gif”, wx.BITMAP_TYPE_GIF)
will also create a 0 size gif on my machine (WIN XP SP3, wx 2.8.9.2-unicode )

If your code is mission critical, use PIL to save to GIF until Robin finds the culprit.

Peter

···

On Mon, Feb 23, 2009 at 2:35 PM, Steven Sproat sproaty@gmail.com wrote:

It works great for every file type except for gifs - it creates a 0-byte image. All the other file formats work correctly, so I’m not sure what’s wrong. I’m using 2.8.9.1, python 2.5.2, Ubuntu 8.10 64bit, kernel 2.6.27-9-generic #1 SMP


wxpython-users mailing list

wxpython-users@lists.wxwidgets.org

http://lists.wxwidgets.org/mailman/listinfo/wxpython-users


There is NO FATE, we are the creators.
blog: http://damoc.ro/

Thanks, it's good to know. I'll just omit GIF as a saveable filetype in the meantime.
Steven

Peter Damoc wrote:

···

I don't think there is something wrong with your code.
a simple
wx.Bitmap("test.png").SaveFile("test.gif", wx.BITMAP_TYPE_GIF)
will also create a 0 size gif on my machine (WIN XP SP3, wx 2.8.9.2-unicode )

If your code is mission critical, use PIL to save to GIF until Robin finds the culprit.

Peter

Steven Sproat wrote:

Thanks, it's good to know. I'll just omit GIF as a saveable filetype in the meantime.

wx does not support writing GIF due to patent issues:

from the wxImage docs:

wxGIFHandler Only for loading, due to legal issues.

I think the patent has finally expired eveywhere, so maybe this will be changed, but PNG is usually a better option anyway.

Though it would be nicer to an exception...

-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

Christopher Barker wrote:

Steven Sproat wrote:

Thanks, it's good to know. I'll just omit GIF as a saveable filetype in the meantime.

wx does not support writing GIF due to patent issues:

from the wxImage docs:

wxGIFHandler Only for loading, due to legal issues.

I think the patent has finally expired eveywhere, so maybe this will be changed, but PNG is usually a better option anyway.

Yep. PNG was created to have a patent free image type when the nonsense about GIF first came out. It then evolved to be a better image format and GIF started to stagnate. When the patents expired it was discussed on wx-dev and the result was that if somebody cared enough about it to submit a patch then we would add write-support, but not bother with it otherwise.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!