获取屏幕像素颜色linux python3 [英] Get screen pixel color linux python3

查看:64
本文介绍了获取屏幕像素颜色linux python3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在python 2.7中有一个正在尝试转换为python 3.3的工作程序.

I have a working program in python 2.7 that I am trying to convert to python 3.3.

工作版本为:

#!/usr/bin/python2
import gtk.gdk
import sys

def PixelAt(x, y):
  w = gtk.gdk.get_default_root_window()
  pb = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, 1, 1)
  cm = w.get_colormap()
  pb = pb.get_from_drawable(w, cm, x, y, 0, 0, 1, 1)
  return pb.pixel_array[0][0]

print(PixelAt(int(sys.argv[1]), int(sys.argv[2])))

部分转换的是:

#!/usr/bin/python3
from gi.repository import Gtk, Gdk, GdkPixbuf
import sys

def PixelAt(x, y):
  w = Gdk.get_default_root_window()
  pb = GdkPixbuf.Pixbuf.new(GdkPixbuf.Colorspace.RGB, False, 8, 1, 1)
  cm = w.get_colormap()                              # What goes here?
  pb = pb.get_from_drawable(w, cm, x, y, 0, 0, 1, 1) # What goes here?
  return pb.pixel_array[0][0]

print(PixelAt(int(sys.argv[1]), int(sys.argv[2])))

我需要完成什么转换?

感谢@jku,这是我完整的python3颜色选择器:

Thanks to @jku here is my complete python3 color picker:

#!/usr/bin/python3
# Print RGB color values of screen pixel at location x, y
from gi.repository import Gdk
import sys

def PixelAt(x, y):
  w = Gdk.get_default_root_window()
  pb = Gdk.pixbuf_get_from_window(w, x, y, 1, 1)
  return pb.get_pixels()

print(tuple(PixelAt(int(sys.argv[1]), int(sys.argv[2]))))

[END-EDIT]

[END-EDIT]

推荐答案

w = Gdk.get_default_root_window()
pb = Gdk.pixbuf_get_from_window(w, x, y, 1 ,1)

应该这样做.请注意, pixbuf_get_from_window()可以返回None,就像您使用的Gdk2函数一样:您必须先检查返回值.

that should do it. Note that pixbuf_get_from_window() can return None just like the Gdk2 function you used: you must check the return value before using it.

这篇关于获取屏幕像素颜色linux python3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆