0

I have a problem with my pixel_at function in my code. Here are all necessary code snippets.

Here is my Helper module:

import gtk.gdk
def pixel_at(x, y):
    gtk.gdk
    rw = gtk.gdk.get_default_root_window()
    pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, 1, 1)
    pixbuf = pixbuf.get_from_drawable(rw, rw.get_colormap(), x, y, 0, 0, 1, 1)
    return tuple(pixbuf.pixel_array[0, 0])

Here is the Controller class for initilaizing:

 import Sensors as sensor
 import Scanner as sc
 import pyautogui
 import time


class GameController(object):
    COLOR_DINOSAUR = (83, 83, 83)

    def __init__(self):
        self.offset = (230, 270) #hardcoded Offset 
        self.width = 600 # hardcoded but alwways the same

        # Stores points (jumps)
        self.points = 0

        #Listeners
        self.onGameEnd = None
        self.onGameStart = None
        self.onSensorData = None

        #Game State
        self.gameState = 'OVER'

        self.sensor = sensor.Sensors()
        self.Scanner = sc.Scanner()

Here is my main function:

import pyautogui
import Helper
import GameController as GC

def main():
     print Helper.pixel_at(25, 5)
     Controller = GC.GameController()
     # Just test values for this function
     Controller.Scanner.scanUntil([75, 124], 
                             (-2, 0),
                             (83, 83, 83),
                             75 / 2)

Here is the code for the Scanner:

import pyautogui
import Helper

 def scanUntil(self, start, step, matchColor, iterLimit):
    iterations = 0
    current = self.makeInBounds(start)

    if step[X] == 0 and step[Y] == 0:
        return None

    while not self.isOutOfBound(current):
        color = Helper.pixel_at(current[X], current[Y])
        # to slow color = pyautogui.pixel(current[X], current[Y])
        if color == matchColor: # Tuple comparison
            return current

        current[X] += step[X]
        current[Y] += step[Y]
        iterations += 1
        if iterations > iterLimit:
            return None

    return None

This line color = Helper.pixel_at(current[X], current[Y]) in the Scanner throws the error: Fatal IO error 0 (Success) on X server :0. But the print Helper.pixel_at(25, 5)call in main() returns me the correct RGB tuple.

The error is thrown at pixbuf = pixbuf.get_from_drawable(rw, rw.get_colormap(), x, y, 0, 0, 1, 1) in the pixel_at(x,y) so I think it has something to do with this.

From the documentation at http://www.pygtk.org/pygtk2reference/class-gdkpixbuf.html#method-gdkpixbuf--get-from-drawable I got this clue: In other words, copies image data from a server-side drawable to a client-side RGB(A) buffer.

But I don´t understand this server-side and client side and why is it working in my first print call but not for color = Helper.pixel_at(current[X], current[Y])?

7Orion7
  • 57
  • 1
  • 7
  • Since I can't answer my own question I write it down here. [_PyGTK back-end does not check $DISPLAY -> not working with Xvfb_](https://pypi.python.org/pypi/pyscreenshot). That was the problem. – 7Orion7 Apr 13 '17 at 08:29
  • 1
    Also worth noting pygtk is a dead project for many years, best to move on to pygobject + gtk3. – TingPing Apr 13 '17 at 20:51

0 Answers0