简体   繁体   English

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

[英]How to copy a image from clipboard in Python?

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() is failing with raise IOError("Unsupported BMP bitfields layout") . ImageGrab.grabclipboard()raise IOError("Unsupported BMP bitfields layout")而失败。 Read in the net that this is a known issue.在网上阅读,这是一个已知问题。 No idea how to fix this.不知道如何解决这个问题。

  2. Next tried ctypes, that is failing with AttributeError: '_FuncPtr' object has no attribute 'save'下一次尝试 ctypes,失败并出现AttributeError: '_FuncPtr' object has no attribute 'save'

  3. bbox is working, but I have no idea of how to make the clipping area dynamic. bbox 正在工作,但我不知道如何使剪切区域动态化。

Whole screen grabbing is working fine整个屏幕抓取工作正常

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

Any help would be great, the idea is to use Snipping Tool to clip the screen and then copy the image from clipboard to a variable and use the savescreenshot method to save it in a folder.任何帮助都会很棒,想法是使用 Snipping Tool 剪切屏幕,然后将图像从剪贴板复制到变量并使用 savescreenshot 方法将其保存在文件夹中。 Any help would be great.任何帮助都会很棒。

For ImageGrab to save the clipboard用于 ImageGrab 保存剪贴板

How!如何!

Run on python==2.7 pillow==2.7.0python==2.7 枕头==2.7.0 上运行

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

Why?为什么?

IOError: Unsupported BMP bitfields layout IOError:不支持的 BMP 位域布局

Reproducible with Pilllow 2.8.0, 2.8.1, 2.8.2.可使用 Pillow 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不能用 Pillow 2.6.0、2.7.0 重现https://github.com/python-pillow/Pillow/issues/1293

Notice注意

sorry to notice that is was only work on windows很抱歉注意到它仅适用于 Windows

The only way I know of is to use gtk .我知道的唯一方法是使用gtk Example例子

window = gtk.screen_get_default().get_root_window()
coordinates = (0, 0) + window.get_size() # (0, 0) is the x and y positions, get_size() is the width and height of the window.
pix = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, *coordinates[2:]) # size of the image
colormap = window.get_colormap()
pix.get_from_drawable(window, colormap, coordinates[0], coordinates[1], 0, 0) # The last four numbers are where in the window we are getting this image and where in the pixbuf we are storing it.  We want the window to be drawn at 0, 0 always, but we want to get the image from wherever we want.  That is decided when we define coordinates.
clipboard = gtk.Clipboard()
clipboard.set_image(pix)

For helpful information on pygtk, see developer.gnome.org有关 pygtk 的有用信息,请参阅developer.gnome.org

Ok.好的。 I finally found a way to solve this issue.我终于找到了解决这个问题的方法。 The problem is existing in Pillow since version 2.8.0自 2.8.0 版本以来,该问题存在于 Pillow 中

AS per this link.根据此链接。 Also as mentioned here the last version in which this worked was 2.7.0同样如此处提到的最后一个版本是 2.7.0

So as the issue is with the BmpImagePlugin.py file, what I did was download the Pillow-2.7.0.tar.gz (md5) from here and replace the BmpImagePlugin.py found in my D:\\Python\\Lib\\site-packages\\PIL with the one found in the downloaded file.因此,由于 BmpImagePlugin.py 文件的问题,我所做的是从这里下载 Pillow-2.7.0.tar.gz (md5) 并替换在我的 D:\\Python\\Lib\\site- 中找到的 BmpImagePlugin.py- package\\PIL 与在下载的文件中找到的那个。 Everything worked perfectly.一切都很完美。

I tried installing 2.7.0 using cmd prompt but was constantly getting some bat file missing error(the issue related to Visual Studio).我尝试使用 cmd 提示符安装 2.7.0,但不断收到一些 bat 文件丢失错误(与 Visual Studio 相关的问题)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM