Advertisement
Peaser

Get color of a pixel on screen Class

Mar 6th, 2015
444
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.98 KB | None | 0 0
  1. class Pixel:
  2.  
  3.     def asHex(color_tuple):
  4.         return "#"+''.join([str(hex(i)).lstrip("0x") for i in color_tuple]).upper()
  5.  
  6.     modal = {
  7.         0: win32gui_PC,
  8.         1: PIL_PC,
  9.         2: xlib_PC,
  10.         3: PyGTK_PC,
  11.         4: PyQt_PC
  12.     }
  13.  
  14.     def win32gui_PC(i_x, i_y):
  15.         import win32gui
  16.         i_desktop_window_id = win32gui.GetDesktopWindow()
  17.         i_desktop_window_dc = win32gui.GetWindowDC(i_desktop_window_id)
  18.         long_colour = win32gui.GetPixel(i_desktop_window_dc, i_x, i_y)
  19.         i_colour = int(long_colour)
  20.         return (i_colour & 0xff), ((i_colour >> 8) & 0xff), ((i_colour >> 16) & 0xff)
  21.  
  22.     def PIL_PC(i_x, i_y):
  23.         import PIL.ImageGrab
  24.         return PIL.ImageGrab.grab().load()[i_x, i_y]
  25.  
  26.     def xlib_PC(i_x, i_y):
  27.         import PIL.Image # python-imaging
  28.         import PIL.ImageStat # python-imaging
  29.         import Xlib.display # python-xlib
  30.         o_x_root = Xlib.display.Display().screen().root
  31.         o_x_image = o_x_root.get_image(i_x, i_y, 1, 1, Xlib.X.ZPixmap, 0xffffffff)
  32.         o_pil_image_rgb = PIL.Image.fromstring("RGB", (1, 1), o_x_image.data, "raw", "BGRX")
  33.         lf_colour = PIL.ImageStat.Stat(o_pil_image_rgb).mean
  34.         return tuple(map(int, lf_colour))
  35.  
  36.     def PyGTK_PC(i_x, i_y):
  37.         import gtk # python-gtk2
  38.         o_gdk_pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, 1, 1)
  39.         o_gdk_pixbuf.get_from_drawable(gtk.gdk.get_default_root_window(), gtk.gdk.colormap_get_system(), i_x, i_y, 0, 0, 1, 1)
  40.         return tuple(o_gdk_pixbuf.get_pixels_array().tolist()[0][0])
  41.  
  42.     def PyQt_PC(i_x, i_y):
  43.         import PyQt4.QtGui # python-qt4
  44.         app = PyQt4.QtGui.QApplication([])
  45.         long_qdesktop_id = PyQt4.QtGui.QApplication.desktop().winId()
  46.         long_colour = PyQt4.QtGui.QPixmap.grabWindow(long_qdesktop_id, i_x, i_y, 1, 1).toImage().pixel(0, 0)
  47.         i_colour = int(long_colour)
  48.         return ((i_colour >> 16) & 0xff), ((i_colour >> 8) & 0xff), (i_colour & 0xff)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement