简体   繁体   中英

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") . 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'

  3. bbox is working, but I have no idea of how to make the clipping area dynamic.

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. Any help would be great.

For ImageGrab to save the clipboard

How!

Run on python==2.7 pillow==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

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

Notice

sorry to notice that is was only work on windows

The only way I know of is to use 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

Ok. I finally found a way to solve this issue. The problem is existing in Pillow since version 2.8.0

AS per this link. Also as mentioned here the last version in which this worked was 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. 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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