I got the program to run without errors by adding a call to image.convert(mode="RGB"); normally, PIL loads things as RGBA, which put_pil_image() doesn't seem to know how to handle.

I've gotten it to draw, but for some reason, unless I do something to the graphics context (like set a foreground/background color) it only draws the image some of the time. If I set the gc's background color, however, it seems to always draw.

Also, the event loop in your app doesn't seem to be spinning; I never get past a single iteration of it.


Here's my version of the code:

#! /usr/bin/env python

import sys
from Xlib import display, X, Xutil
import Image

class Window(object):
    def __init__(self, display, image_path=None):
        self.display = display
        self.screen = self.display.screen()

        self.width = self.screen.width_in_pixels
        self.height = self.screen.height_in_pixels
        self.depth = self.screen.root_depth

        self.window = self.screen.root.create_window(0, 0, self.width, self.height, 0, self.depth)

        self.window.map()

        self.gc = self.window.create_gc(foreground=self.screen.white_pixel)

        # Load the image.
        self.image = None
        if image_path:
            self.image = Image.open(image_path).resize((self.width, self.height))
            # Convert image to RGB format. (remove alpha channel, since PIL seems to load that on most images)
            self.image = self.image.convert(mode="RGB")

        # Set the background color. (For some reason, this makes it work consistently. I don't understand why this is needed.)
        self.colormap = self.screen.default_colormap
        self.red = self.colormap.alloc_named_color("red").pixel
        self.gc.change(background=self.red)

        # Draw the image, if loaded.
        if self.image is not None:
            self.window.put_pil_image(self.gc, 0, 0, self.image)

    def loop(self):
        while True:
            event = self.display.next_event()

            if event.type == X.DestroyNotify:
                sys.exit(0)

if __name__ == '__main__':
    Window(display.Display(), sys.argv[1]).loop()



David H. Bronke
Vice President of Project Development
G33X Nexus Entertainment
http://people.g33xnexus.com/whitelynx/

v4sw7+8Yhw5/7ln4/6pr7Ock3ma7u7/8Lw3/7Xm3/5l7GUi2e4/6t2/4MGBSb7HODune/en6g8+9OZARa25s7/8MIr2+7p-3.5/-4.21
Support Web Standards! http://www.webstandards.org/
Chat: AIM: FunkieMous
Facebook DeviantART Twitter


On Thu, Dec 3, 2009 at 11:13, Maxime Ripard <maxime.ripard@anandra.org> wrote:
Hi,

I'm new to Xlib, and I'm trying to do a very basic thing, without
succeeding. I'm willing to display in a fullscreen window an image
passed as argument. But somehow, I'm obviously missing something.

If you could take a look, I'm running out of ideas here.

Thanks

Here is the code :

#! /usr/bin/env python

import sys
from Xlib import display, X, Xutil
import Image

class Window(object):
   def __init__(self, display, image_path = None):
       self.display = display
       self.screen = self.display.screen()

       self.width = self.screen.width_in_pixels
       self.height = self.screen.height_in_pixels
       self.depth = self.screen.root_depth

       self.window = self.screen.root.create_window(0, 0,
               self.width, self.height, 0, self.depth)

       self.gc = self.window.create_gc(foreground =
               self.screen.white_pixel)

       if image_path:
           image = Image.open(image_path)
           self.window.put_pil_image(self.gc, 0, self.height,
               image.resize((self.width, self.height)))

       self.window.map()

   def loop(self):
       while True:
           event = self.display.next_event()

           if event.type == X.DestroyNotify:
               sys.exit(0)

if __name__ == '__main__':
   Window(display.Display(), sys.argv[1]).loop()


--
Maxime Ripard
Étudiant à l'Université de Technologie de Belfort - Montbéliard (UTBM)
Génie Informatique (GI 04)

6bis rue Paul Giroud
90000 Belfort
France

06 85 40 57 32

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAksX8boACgkQGxsu9jQV9nbp+ACcDsqWG5pjBwi0wEqDhZsfklfu
988An0JkziLa2Td1JXVa3wwrizy+IZsf
=V68K
-----END PGP SIGNATURE-----

------------------------------------------------------------------------------
Join us December 9, 2009 for the Red Hat Virtual Experience,
a free event focused on virtualization and cloud computing.
Attend in-depth sessions from your desk. Your couch. Anywhere.
http://p.sf.net/sfu/redhat-sfdev2dev
_______________________________________________
python-xlib-users mailing list
python-xlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/python-xlib-users