[英]Get screen pixel color linux python3
I have a working program in python 2.7 that I am trying to convert to python 3.3.我在 python 2.7 中有一个工作程序,我正在尝试将其转换为 python 3.3。
The working version is:工作版本是:
#!/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])))
The partly converted one is:部分转换的一个是:
#!/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])))
What do I need to finish converting?我需要什么才能完成转换?
[EDIT] [编辑]
Thanks to @jku here is my complete python3 color picker:感谢@jku,这里是我完整的 python3 颜色选择器:
#!/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] [结束编辑]
w = Gdk.get_default_root_window()
pb = Gdk.pixbuf_get_from_window(w, x, y, 1 ,1)
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.请注意, pixbuf_get_from_window()
可以像您使用的 Gdk2 函数一样返回 None:您必须在使用之前检查返回值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.