0

I want to convert clipboard image content into an image

I tried

from PIL import ImageGrab
im = ImageGrab.grabclipboard()
im.save(filename)

it says ImageGrab.grabclipboard() is macOS and Windows only

AboodXD
  • 148
  • 9
Santhosh
  • 9,965
  • 20
  • 103
  • 243
  • An expression like `im = foo() is x` will make `im` a boolean btw. Not sure booleans have a save method. Plus thinking maybe there is some missing quotations in sample code. Assuming its `"macOS and Windows only"` then I'm pretty sure `im` is gonna be `False`. Either way, its clear this will not do what you are thinking. – Andrew Allaire May 17 '21 at 18:38
  • @AndrewAllaire clearly it's just a mistake where they accidentally pasted the error message in the code. – AboodXD May 17 '21 at 22:41
  • Simple Google search shows that this is a duplicate: https://stackoverflow.com/questions/6841532/linux-image-from-clipboard/27995840 – AboodXD May 17 '21 at 22:45
  • i tried this answer https://stackoverflow.com/a/12122028/2897115. i get `No module named gtk`. I am not sure anything changed since 2012 (the question was answered) and now – Santhosh May 18 '21 at 01:47
  • https://python-gtk-3-tutorial.readthedocs.io/en/latest/install.html – AboodXD May 18 '21 at 02:38
  • currnetly i solved this using `xclip` and `os.system` – Santhosh May 18 '21 at 03:13

1 Answers1

1

Currently I have to use xclip to solve this problem

it will try to save image from clipboard and if not it will return 1

    import os
    if os.system(f"xclip -selection clipboard -target image/png -out > {filename}") == 0:
        print("save")
        return 0
    else:
        return 1
Santhosh
  • 9,965
  • 20
  • 103
  • 243