3

我在 Linux 上,想尝试为我的 Web 开发重新创建 Nattyware 的 Pixie 工具。gPick 还可以,但 Pixie 更好。

我希望能够检测并显示鼠标指针周围的区域。我一直在尝试找到一种方法来显示鼠标指针周围的区域,并用 Python 放大。

我不知道从哪里开始这样的事情。我不想保存任何图像,只是显示鼠标在窗口中的放大区域。

编辑:我得到了一些可能有效的东西。不要运行它,它会崩溃!

import sys, evdev
from Xlib import display, X
from PyQt4 import QtGui
from PyQt4.QtGui import QPixmap, QApplication, QColor

class printImage():
def __init__(self):
    self.window = QtGui.QMainWindow()
    self.window.setGeometry(0,0,400,200)

    self.winId = QApplication.desktop().winId()
    self.width = 150
    self.height = 150

    self.label = QtGui.QLabel('Hi')
    self.label.setGeometry(10, 10, 400, 100)
    self.label.show()

def drawView(self, x, y):
    self.label.setText('abc')
    pix = self.getScreenArea(x, y)
    self.pic.setPixmap(pix)

def render(self):
    self.window.show()

def getScreenArea(self, areaX, areaY):
    image = QPixmap.grabWindow(
        self.winId,
        x = areaX,
        y = areaY,
        width = self.width,
        height = self.height
    )

    return image

if __name__ == '__main__':
    app = QApplication(sys.argv)
    view = printImage()

    view.render()

    display = display.Display(':0')
    root = display.screen().root

    root.grab_pointer(
       True, 
       X.PointerMotionMask | X.ButtonReleaseMask, 
       X.GrabModeAsync, 
       X.GrabModeAsync, 
       0, 0, 
       X.CurrentTime
   )

    while True:
        ev = display.next_event()
        view.drawView(ev.event_x, ev.event_y)

    app.exec_()

知道为什么它会自我毁灭吗?它在grabWindow() 函数上崩溃.. 还有什么我可以使用的吗?

4

2 回答 2

2

这在 linux 上对我有用,可能是跨平台的:

import wx
ff=wx.App()
screen = wx.ScreenDC()
size = screen.GetSize()
bmp = wx.EmptyBitmap(size[0], size[1])
mem = wx.MemoryDC(bmp)
mem.Blit(0, 0, size[0], size[1], screen, 0, 0)
del mem
#bmp.SaveFile('screenshot.png', wx.BITMAP_TYPE_PNG)
im = bmp.ConvertToImage()

来自help

ConvertToImage

Creates a platform-independent image from a platform-dependent
bitmap. This preserves mask information so that bitmaps and images can
be converted back and forth without loss in that respect.
于 2012-06-11T22:59:30.877 回答
0

我得到了它。我没有使用 while 循环,而是切换到 QTimer。这释放了大量的内存使用,grabWindow() 执行得非常好。

import sys, pymouse
from PyQt4.QtGui import *
from PyQt4.QtCore import *

class printImage(QWidget):
    def __init__(self, *args):
        apply(QWidget.__init__,(self, ) + args)
        QWidget.__init__(self)

        self.winId = QApplication.desktop().winId()
        self.mouse = pymouse.PyMouse()
        self.timer = QTimer()

        self.x = self.y = self.cX = self.cY = 0

        self.createLabel()
        self.show()
        self.startListening()

    def createLabel(self):
        self.label = QLabel(self)
        self.label.move(10, 15)
        self.label.resize(150, 130)

    def startListening(self):
        self.timer.connect(self.timer, SIGNAL('timeout()'), self.sendData)
        self.timer.start(0)

    def sendData(self):
        pos = self.mouse.position()
        x = pos[0]
        y = pos[1]

        if (self.cX != x) and (self.cY != y):
            self.x = self.cX = x
            self.y = self.cY = y

            self.label.setPixmap(
                self.cropScreenArea()
            )

    def cropScreenArea(self):
        return QPixmap.grabWindow(
            self.winId,
            x = self.x - 80,
            y = self.y - 60,
            width = 150,
            height = 130
        )

    def keyPressEvent(self, e):
        if e.key() == Qt.Key_Escape:
            self.close()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    view = printImage()

    sys.exit(app.exec_())
于 2012-06-12T19:32:16.223 回答