Skip to content
This repository

Problem loading some images #516

Closed
duelafn opened this Issue June 19, 2012 · 7 comments

4 participants

Dean Serenevy Mathieu Virbel gau-veldt Edwin Marshall
Dean Serenevy

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/img_pygame.py
+++ b/kivy/core/image/img_pygame.py
@@ -52,10 +52,14 @@ class ImageLoaderPygame(ImageLoaderBase):
                 imc = im.convert(32)
                 fmt = 'rgba'
             except:
-                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 kivy.app import App
from kivy.uix.image import Image
from kivy.graphics import Rectangle, Color


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

if __name__ in ('__main__', '__android__'):
    ImageTest().run()

test images (as far as I can tell, the only significant difference between these is the color palette):

gau-veldt

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.

gau-veldt

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:

screenshot: https://dl.dropbox.com/u/55284551/screenshot_4fdc7a8f969f7.png
image: https://dl.dropbox.com/u/55284551/4fdc7a8f969f7.jpg

Dean Serenevy

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

gau-veldt

@duelafn I concur that the problem is probably in pygame however in my case it is not a 1-bit image causing issue.

Edwin Marshall
Collaborator

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

gau-veldt
Mathieu Virbel
Owner
tito commented June 29, 2012

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

Mathieu Virbel tito closed this in a1af1b8 June 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.