如何在Python中从剪贴板复制图像?

Dip*_*jee 4 python ctypes python-imaging-library

def Clip(self):
    subprocess.call('SnippingTool.exe')
    #ctypes.windll.user32.OpenClipboard(0)
    #ClippedScreen=ctypes.windll.user32.GetClipboardData
    #ClippedScreen=PIL.ImageGrab.grab(bbox=(10,10,500,500))
    ClippedScreen = PIL.ImageGrab.grabclipboard()
    self.savescreenshot(ClippedScreen)
Run Code Online (Sandbox Code Playgroud)
  1. ImageGrab.grabclipboard()失败了raise IOError("Unsupported BMP bitfields layout")。在网上读到这是一个已知问题。不知道如何解决这个问题。

  2. 接下来尝试了 ctypes,但由于AttributeError: '_FuncPtr'对象没有属性“保存”而失败

  3. bbox 正在工作,但我不知道如何使剪切区域动态。

全屏抓取工作正常

def Prntscrn(self):
            WholeScreen=ImageGrab.grab()
            self.savescreenshot(WholeScreen)
Run Code Online (Sandbox Code Playgroud)

任何帮助都会很棒,想法是使用截图工具剪辑屏幕,然后将图像从剪贴板复制到变量并使用 savescreenshot 方法将其保存在文件夹中。任何帮助都会很棒。

Yao*_*Yao 5

让 ImageGrab 保存剪贴板

如何!

在python==2.7pillow==2.7.0上运行

from PIL import ImageGrab, Image
im= ImageGrab.grabclipboard()
if isinstance(im, Image.Image):
    im.save('tmp.jpg')
Run Code Online (Sandbox Code Playgroud)

为什么?

IOError:不支持的 BMP 位域布局

可使用 Pillow 2.8.0、2.8.1、2.8.2 重现。Pillow 2.6.0、2.7.0 无法重现 https://github.com/python-pillow/Pillow/issues/1293

注意

抱歉,这仅适用于 Windows