Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 252
Dip Chatterjee Avatar asked Sep 05 '25 07:09

Dip Chatterjee


1 Answers

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

like image 75
Yao Avatar answered Sep 07 '25 21:09

Yao