I would just use Pillow:
from PIL import ImageGrab
im = ImageGrab.grabclipboard()
im.save('somefile.png','PNG')
Answer from Gerrat on Stack OverflowI would just use Pillow:
from PIL import ImageGrab
im = ImageGrab.grabclipboard()
im.save('somefile.png','PNG')
You need to pass a parameter to GetClipboardData specifying the format of the data you're looking for. You can use EnumClipboardFormats to see the formats that are available - when I copy something in Explorer there are 15 formats available to me.
Edit 2: Here's the code to get a filename after you've copied a file in Explorer. The answer will be completely different if you've copied an image from within a program, a browser for example.
import win32clipboard
win32clipboard.OpenClipboard()
filename_format = win32clipboard.RegisterClipboardFormat('FileName')
if win32clipboard.IsClipboardFormatAvailable(filename_format):
input_filename = win32clipboard.GetClipboardData(filename_format)
win32clipboard.CloseClipboard()
Edit 3: From the comments it's clear you have an actual image in the clipboard, not the filename of an image file. You've stated that you can't use PIL, so:
import win32clipboard
win32clipboard.OpenClipboard()
if win32clipboard.IsClipboardFormatAvailable(win32clipboard.CF_DIB):
data = win32clipboard.GetClipboardData(win32clipboard.CF_DIB)
win32clipboard.CloseClipboard()
At this point you have a string (in Python 2) or bytes (in Python 3) that contains the image data. The only format you'll be able to save is .BMP, and you'll have to decode the BITMAPINFOHEADER to get the parameters for a BITMAPFILEHEADER that needs to be written to the front of the file.
» pip install pyperclipimg
python - Copy image to clipboard? - Stack Overflow
How to copy an image to clipboard with python
How to copy a image from clipboard in Python? - Stack Overflow
How to copy file to clipboard
I did copy the code and replace the StringIO with BytesIO and it worked! (with *.jpg and *.png files) Thank you so much!
from io import BytesIO
import win32clipboard
from PIL import Image
def send_to_clipboard(clip_type, data):
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardData(clip_type, data)
win32clipboard.CloseClipboard()
filepath = 'Ico2.png'
image = Image.open(filepath)
output = BytesIO()
image.convert("RGB").save(output, "BMP")
data = output.getvalue()[14:]
output.close()
send_to_clipboard(win32clipboard.CF_DIB, data)
You don't want StringIO here. Images are raw binary data, and in Py3, str is purely for text; bytes and bytes-like objects (bytearray, contiguous memoryviews, mmaps) are for binary data. To replace Py2's StringIO.StringIO for binary data, you want to use io.BytesIO in Python 3, not io.StringIO.
Hi,
I am making a program that copies the an images from the downloads folder as soon as I download something from the internet. I am using watchdogs module for observing the folder but the problem is I don’t know how to copy an image to clipboard. There is no good solution on the internet for this problem. PLEASE HELP GUYS ! I Will award the best answer 😇
scrot + xclip
You can use scrot with xclip to take a screenshot and copy it to clipboard.
scrot '/tmp/%F_%T_$wx$h.png' -e 'xclip -selection clipboard -target image/png -i $f'
It will capture whole of your screen and copy the image to clipboard. If you want to capture current window then use -u flag. For selection of particular area, you can add -s flag. See $ man scrot for more options.
It will store your screenshot in /tmp directory. You can change that directory wherever you want it to get stored. Files from /tmp directory usually gets deleted after each reboot. If you want to immediately remove the stored file, then do something like:
scrot -w '/tmp/%F_%T_$wx$h.png' -e 'xclip -selection clipboard -target image/png -i $f && rm $f'
As I read in other comments, you need it for copying a screenshot to the clipboard. I hope this answers your question.
If you just need to copy an already existing image file to clipboard:
cat 2018-06-16-224938_670x730_scrot.png | xclip -selection clipboard -target image/png -i
You can set keyboard shortcuts/keybindings according to your current Desktop Environment/window manager.
Bonus
Explanation of /tmp/%F_%T_$wx$h.png:
It's being used as the file name. These are called format specifiers. They are of two type: starting with % or $.
%F Equivalent to %Y-%m-%d (the ISO 8601 date format). %T The time in 24-hour notation (%H:%M:%S).
%F_%T_ will print something like: 2018-06-17_02:52:19_ i.e. your current timestamp. You can customize the format as per your requirements. See $ man strftime for more help.
$wx$h are part of the scrot's internal specifiers.
$w image width $h image height
So the final file name will look something like 2018-06-17_02:52:19_1365x384.png.
First, install python, and pygtk
sudo apt-get install python pygtk
Now save the following script somewhere as imgclip.py (see https://stackoverflow.com/questions/3571855/pasting-image-to-clipboard-in-python-in-linux)
#! /usr/bin/python
import pygtk
pygtk.require('2.0')
import gtk
import os
import sys
def copy_image(f):
assert os.path.exists(f), "file does not exist"
image = gtk.gdk.pixbuf_new_from_file(f)
clipboard = gtk.clipboard_get()
clipboard.set_image(image)
clipboard.store()
copy_image(sys.argv[1]);
To use it:
python /path/to/imgclip.py filename.png
Note: tested pasting in gimp and xournal. Note: this is for gnome desktop (hence gtk). I bet there's something similar for kde
» pip install jaraco.clipboard
xclip
xclip (man page) is on official repos of major distros (pasteimg it isn't and last update was on 2011).
For me the quickest way is using terminal, going to the desired folder (maybe using Z) and then just run clipboard2photo, which is an alias I created to the following command:
xclip -selection clipboard -t image/png -o > "$(date +%Y-%m-%d_%T).png" # Use "Screenshot from $(date "+%Y-%m-%d %H-%M-%S").png" if you like GNOME filename format.
To save in JPEG format instead of PNG using imagemagick:
f="$(date '+%Y-%m-%d_%T').png" && xclip -selection clipboard -t image/png -o > "$f" && mogrify -format jpg "$f" && rm "$f" && unset "f"
With something on clipboard you can get all possible targets with: xclip -selection clipboard -o -t TARGETS.
GNOME Files/Nautilus
GNOME v44 let you paste directly from clipboard to Files/Nautilus.
GNOME Shell shortcuts
On GNOME Shell (Ubuntu 17.10+) we have built in shortcuts to save screenshots directly to ~/Pictures:

Hints to remember them:
- Ctrl → to clipboard (else to
~/Pictures) - Alt → Current window
- Shift → Area/Surface
I usually disable "Print" to avoid generating garbage on ~/Pictures if I miss F12 (which I use a lot with Guake) and accidentally press it.
Tip 1: Put your aliases on ~/sync_folder/.mybashrc (or maybe better .myprofile; where sync_folder might be Nextcloud or any other service) and then include this file from .bashrc.
[ -r ~/sync_folder/.mybashrc ] && source ~/sync_folder/.mybashrc
This way you will have them on all your current and future devices. Of course, you can also have a ~/.dotfiles with a git repo.
Tip 2: In the same way, you can: thisOutputMarkdown | pandoc -s -f markdown -t html | xclip -selection clipboard -t text/html to get formatted HTML to clipboard.
If you have the Markdown text on clipboard replace thisOutputMarkdown with xsel -b.
Wayland
It seems there is some work to be done about xclip on Wayland (echo $XDG_SESSION_TYPE). Please help developers on this issue. Meanwhile, maybe wl-paste (from wl-clipboard) might work:
wl-paste -t image/png > "$(date +%Y-%m-%d_%T).png"
I just threw together a quick python script that will paste a clipboard image to a file.
It's very basic but it does the job but could be easily extended.
PasteImg
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