gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 05:16:07 25 4
gpt4 key购买 nike

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)
  1. ImageGrab.grabclipboard() 失败,并引发 IOError("不支持的 BMP 位字段布局")。在网上读到这是一个已知问题。不知道如何解决这个问题。

  2. 接下来尝试了 ctypes,但失败并出现 AttributeError: '_FuncPtr' object has no attribute 'save'

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

全屏抓取工作正常

def Prntscrn(self):
WholeScreen=ImageGrab.grab()
self.savescreenshot(WholeScreen)

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

最佳答案

让ImageGrab保存剪贴板

如何!

python==2.7pillow==2.7.0上运行

from PIL import ImageGrab, Image
im= ImageGrab.grabclipboard()
if isinstance(im, Image.Image):
im.save('tmp.jpg')

为什么?

IOError: Unsupported BMP bitfields layout

Reproducible with Pilllow 2.8.0, 2.8.1, 2.8.2. Not reproducible with Pillow 2.6.0, 2.7.0 https://github.com/python-pillow/Pillow/issues/1293

通知

sorry to notice that is was only work on windows

关于python - 如何在Python中从剪贴板复制图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35254655/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com