Skip to content

Problem loading some images #516

duelafn opened this Issue Jun 19, 2012 · 7 comments

4 participants

duelafn commented Jun 19, 2012

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")
>>> im.get_palette()
((0, 0, 0), (255, 255, 255))
>>> im2 = pygame.image.load("brick.png")
>>> im2.get_palette()
((0, 0, 0), (0, 0, 0))
>>> pygame.display.init()
>>> pygame.display.set_mode((640,480))
<Surface(640x480x32 SW)>
>>> im.convert()
<Surface(24x24x32 SW)>
>>> im2.convert(32)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
pygame.error: Empty destination palette
>>> im2.convert_alpha()
<Surface(469x137x32 SW)>

One possible fix is to attempt a .convert_alpha() if the .convert(32) fails:

--- a/kivy/core/image/
+++ b/kivy/core/image/
@@ -52,10 +52,14 @@ class ImageLoaderPygame(ImageLoaderBase):
                 imc = im.convert(32)
                 fmt = 'rgba'
-                Logger.warning(
-                    'Image: Unable to convert image <%s> to rgba (was %s)' %
-                    filename, im.fmt)
-                raise
+                try:
+                    imc = im.convert_alpha()
+                    fmt = 'rgba'
+                except:
+                    Logger.warning(
+                        'Image: Unable to convert image <%s> to rgba (was %s)' %
+                        filename, im.fmt)
+                    raise
             im = imc

         # update internals

Simple test script:

import kivy

from import App
from kivy.uix.image import Image
from import Rectangle, Color

class ImageTest(App):
    def build(self):
        img = Image(source="brick.png")
        with img.canvas.before:
            rec = Rectangle(size=img.size)
        def resize_rec(*args):
        return img

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
Kivy 1.3.0

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:


duelafn commented Jun 27, 2012

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:

export KIVY_IMAGE=pil 

On *nix, for example. More info here

Kivy member
tito commented Jun 29, 2012

Agreed to apply this patch. But your issue is not the same gau-veldt :)

@tito tito closed this in a1af1b8 Jun 29, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.