export_to_png with Python 3.3 reverses red/green channels

95 views
Skip to first unread message

Matthew C

unread,
Aug 20, 2014, 3:34:39 AM8/20/14
to kivy-...@googlegroups.com
While using the new export_to_png method for Widget in kivy 1.8.1 I've stumbled upon two problems:
1) No transparency for pngs. This is more of a design choice than a bug, as clearing to black for snapshots is desirable in some cases.
2) The red and green channels are flipped.

The first problem is easily solved by going to kivy/kivy/uix/widget.py.
Inside the export_to_png method change ClearColor(0, 0, 0, 1) to ClearColor(0, 0, 0, 0).
You can also use this to change the background color if you're exporting to a different format.

The second problem is a bug with pyGame for Python 3.3 rather than kivy itself. The bug only applies to png export (jpg and bmp work fine) and it does not exist in the Python 2.7 build.

I'm stumped as to where I can fix the problem since I can't directly access pygame's image source (it's a .pyd) nor can I modify the bytes before they're thrown it to pygame which gets it wrong.
I suppose it's possible to unpack and repack texture.pixels but I'm not really sure what format it's in.
Can somebody please push me in the right direction?

Alexander Taylor

unread,
Aug 20, 2014, 8:08:36 AM8/20/14
to kivy-...@googlegroups.com
The pixels format is a byte array, probably each 4 bytes is the rgba colour. You can probably just play with this to test it - if you can work out how to extract the pixels and re-upload them (or add theem to a new texture), you can play with swapping or replacing parts just to see what happens.

You could also do sillier but effective things like giving the export fbo a shader that flips the colours, so that kivy renders te colours wrong in the rendering and pygame flips them back again.

Matthew C

unread,
Aug 20, 2014, 2:17:44 PM8/20/14
to kivy-...@googlegroups.com
Thanks for the prompt response! During the downtime the site that hosts unofficial pyGame builds (http://www.lfd.uci.edu/~gohlke/pythonlibs/) came back up. His build for Python 3.4 does not have this bug, and it also allows you to use kivy1.8.1dev with Python 3.4.1. It's likely his python3.3 build functions correctly as well, in case you don't want to upgrade.

Apologies for the cop-out solution. Have bigger programming tasks to tackle at the moment...
Reply all
Reply to author
Forward
0 new messages