I'm not sure if this is a bug in kivy or in pygame, but some png images can fail to load. I am encountering this with 1-bit pngs where both colors in the color palette are black. In this case, "im.convert(32)" will raise an error: "pygame.error: Empty destination palette". However, "im.convert_alpha()" will produce a usable image.
>>> import pygame
>>> im = pygame.image.load("city.png")
((0, 0, 0), (255, 255, 255))
>>> im2 = pygame.image.load("brick.png")
((0, 0, 0), (0, 0, 0))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
pygame.error: Empty destination palette
One possible fix is to attempt a .convert_alpha() if the .convert(32) fails:
@@ -52,10 +52,14 @@ class ImageLoaderPygame(ImageLoaderBase):
imc = im.convert(32)
fmt = 'rgba'
- 'Image: Unable to convert image <%s> to rgba (was %s)' %
- filename, im.fmt)
+ imc = im.convert_alpha()
+ fmt = 'rgba'
+ 'Image: Unable to convert image <%s> to rgba (was %s)' %
+ filename, im.fmt)
im = imc
# update internals
Simple test script:
from kivy.app import App
from kivy.uix.image import Image
from kivy.graphics import Rectangle, Color
img = Image(source="brick.png")
rec = Rectangle(size=img.size)
if __name__ in ('__main__', '__android__'):
test images (as far as I can tell, the only significant difference between these is the color palette):
Some images also load incorrectly on my system (they load fine in every other app I tried them in, windows photo viewer, GIMP, FireFox, etc...).
They appear in the Image widget with part of the image garbled, usually at the top. Sometimes the image is checkerboarded, meaning every other pixel is incorrect and it altenates one pixel over line-to-line (hence the checkerboard pattern that looks like dithering of old EGA days), other times it shows with (incorrect) color bands, or vertical lines in the image.
My System info is in #538
Here is an example image and a screenshot PNG indicating the corruption when loaded into a Kivy Image widget, compared to viewing in Windows Photo Viewer:
After reading the pygame docs on convert() and convert_alpha(), I'm feeling like it is most likely a bug over there. I have confirmed that the issue exists on the most recent pygame and opened pygame issue 131
@duelafn I concur that the problem is probably in pygame however in my case it is not a 1-bit image causing issue.
btw, if you ever want to isolate a provider as an issue, you can force kivy to use a different one:
On *nix, for example. More info here
Agreed to apply this patch. But your issue is not the same gau-veldt :)
img: fix crash when loading 1bit image (pygame). closes #516