Question: How to add/get image data in the OS clipboard using Python?

I show only get:
This example is using the built-in Tkinter module to get image data from CLIPBOARD.
Tested only on Linux, but should be a cross-platform solution.

Note: The first paste of the shown 387x388 GIF, takes 4 seconds.


Core point: You have to use a MIME-Type, to requests a image.

.clipboard_get(type='image/png')

Verfied with, 'GIF', 'PNG' and 'JPEG', as source image data, using application, GIMP and PyCharm. With type='image/png' you allways get image data of type 'PNG' if the source app support this.


Reference:

  • clippboard_get(type=<string>)

    Retrieve data from the clipboard. Type specifies the form in which the data is to be returned and should be an atom name such as STRING or FILE_NAME. Type defaults on modern X11 systems to UTF8_STRING.


Data format:

0x89 0x50 0x4e 0x47 0xd 0xa 0x1a 0xa 0x0 0x0 0x0 0xd 0x49 0x48 0x44

The data is divided into fields separated by white space; each field is converted to its atom value, and the 32-bit atom value is transmitted instead of the atom name.

After removing the white space and casting with int(<field>, 0):

bytearray(b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHD...')
<PIL.PngImagePlugin.PngImageFile image mode=P size=387x388 at 0xF555E20C>

Exception: If no selection at all or the source app does not provide 'image/png'.

TclError:CLIPBOARD selection doesn't exist or form "image/png" not defined

import tkinter as tk
from PIL import Image, ImageTk
import io


class App(tk.Tk):
    def __init__(self):
        super().__init__()  # options=(tk.Menu,))
        self.menubar = tk.Menu()
        self.config(menu=self.menubar)
        self.menubar.add_command(label='paste', command=self.on_paste)
        
        self.label = tk.Label(self, text="CLIPBOARD image", font=("David", 18),
                              image='', compound='center')
        self.label.grid(row=0, column=0, sticky='w')

    def on_paste(self):
        self.label.configure(image='')
        self.update_idletasks()
        
        try:
            b = bytearray()
            h = ''
            for c in self.clipboard_get(type='image/png'):
                if c == ' ':
                    try:
                        b.append(int(h, 0))
                    except Exception as e:
                        print('Exception:{}'.format(e))
                    h = ''
                else:
                    h += c

        except tk.TclError as e:
            b = None
            print('TclError:{}'.format(e))
        finally:
            if b is not None:
                with Image.open(io.BytesIO(b)) as img:
                    print('{}'.format(img))
                    self.label.image = ImageTk.PhotoImage(img.resize((100, 100), Image.LANCZOS))
                    self.label.configure(image=self.label.image)

Tested with Python: 3.5 - 'TclVersion': 8.6 'TkVersion': 8.6

Answer from stovfl on Stack Overflow
🌐
Stack Overflow
stackoverflow.com › questions › 76685261 › copying-image-using-python-pyperclip
Copying Image Using Python Pyperclip - Stack Overflow
from PIL import Image import pyperclip # Open the image file image_path = 'img.png' image = Image.open(image_path) # Copy to the clipboard pyperclip.copy(image)
🌐
PyPI
pypi.org › project › pyperclipimg
pyperclipimg · PyPI
To copy an image in a file to the clipboard, pass the filename as a str or Path object to copy(). A Pillow Image object can also be passed. Any image format that Pillow supports can be used (png, jpg, gif, bmp, webp) import pyperclipimg as pci ...
      » pip install pyperclipimg
    
Published   Dec 17, 2024
Version   0.2.0
🌐
GitHub
github.com › asweigart › pyperclip › issues › 198
Copying images · Issue #198 · asweigart/pyperclip
April 26, 2021 - There was an error while loading. Please reload this page · Can I copy the image to the clipboard
Author   SuperZombi
Top answer
1 of 5
1

Question: How to add/get image data in the OS clipboard using Python?

I show only get:
This example is using the built-in Tkinter module to get image data from CLIPBOARD.
Tested only on Linux, but should be a cross-platform solution.

Note: The first paste of the shown 387x388 GIF, takes 4 seconds.


Core point: You have to use a MIME-Type, to requests a image.

.clipboard_get(type='image/png')

Verfied with, 'GIF', 'PNG' and 'JPEG', as source image data, using application, GIMP and PyCharm. With type='image/png' you allways get image data of type 'PNG' if the source app support this.


Reference:

  • clippboard_get(type=<string>)

    Retrieve data from the clipboard. Type specifies the form in which the data is to be returned and should be an atom name such as STRING or FILE_NAME. Type defaults on modern X11 systems to UTF8_STRING.


Data format:

0x89 0x50 0x4e 0x47 0xd 0xa 0x1a 0xa 0x0 0x0 0x0 0xd 0x49 0x48 0x44

The data is divided into fields separated by white space; each field is converted to its atom value, and the 32-bit atom value is transmitted instead of the atom name.

After removing the white space and casting with int(<field>, 0):

bytearray(b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHD...')
<PIL.PngImagePlugin.PngImageFile image mode=P size=387x388 at 0xF555E20C>

Exception: If no selection at all or the source app does not provide 'image/png'.

TclError:CLIPBOARD selection doesn't exist or form "image/png" not defined

import tkinter as tk
from PIL import Image, ImageTk
import io


class App(tk.Tk):
    def __init__(self):
        super().__init__()  # options=(tk.Menu,))
        self.menubar = tk.Menu()
        self.config(menu=self.menubar)
        self.menubar.add_command(label='paste', command=self.on_paste)
        
        self.label = tk.Label(self, text="CLIPBOARD image", font=("David", 18),
                              image='', compound='center')
        self.label.grid(row=0, column=0, sticky='w')

    def on_paste(self):
        self.label.configure(image='')
        self.update_idletasks()
        
        try:
            b = bytearray()
            h = ''
            for c in self.clipboard_get(type='image/png'):
                if c == ' ':
                    try:
                        b.append(int(h, 0))
                    except Exception as e:
                        print('Exception:{}'.format(e))
                    h = ''
                else:
                    h += c

        except tk.TclError as e:
            b = None
            print('TclError:{}'.format(e))
        finally:
            if b is not None:
                with Image.open(io.BytesIO(b)) as img:
                    print('{}'.format(img))
                    self.label.image = ImageTk.PhotoImage(img.resize((100, 100), Image.LANCZOS))
                    self.label.configure(image=self.label.image)

Tested with Python: 3.5 - 'TclVersion': 8.6 'TkVersion': 8.6

2 of 5
0

I don't think you could interact with the clipboard without external module.

Clipboard APIs are different from different Operating systems.

I suggest you to use the clipboard module.

https://pypi.python.org/pypi/clipboard/0.0.4

🌐
Note.nkmk.me
note.nkmk.me › home › python
Copy and Paste Text to the Clipboard with pyperclip in Python | note.nkmk.me
January 30, 2024 - pandas: Copy DataFrame to the clipboard with to_clipboard() As will be discussed in the final section, pyperclip is limited to handling only text (str). You can get the image from the clipboard using Pillow.
🌐
GitHub
github.com › asweigart › pyperclipimg
GitHub - asweigart/pyperclipimg: Cross-platform copy() and paste() functions for images.
>>> import pyperclip as pci >>> from pathlib import Path >>> pci.copy(Path('example.png')) # Copy by Path object. >>> import pyperclip as pci >>> from PIL import Image >>> pci.copy(Image.open('example.png')) # Copy by Image object.
Starred by 7 users
Forked by 2 users
Languages   Python 100.0% | Python 100.0%
🌐
DevDungeon
devdungeon.com › content › grab-image-clipboard-python-pillow
Grab Image from Clipboard in Python with Pillow | DevDungeon
October 28, 2018 - Or, if you right click an image in a web browser and choose "Copy Image". As always, refer to the Check out the Python Pillow package documentation for the most complete reference.
🌐
Quora
quora.com › Can-we-get-an-image-from-local-storage-to-clipboard-with-Python-or-a-little-program-How-can-I-use-https-xpshort-com-nircmd-with-this
Can we get an image from local storage to clipboard with Python or a little program? How can I use https://xpshort.com/nircmd with this? - Quora
Answer: Yes, you can get an image from local storage to clipboard using the [code ]pyperclip[/code] library in Python. Here is an example of how to do this: [code]pythonCopy codeimport pyperclip with open("path/to/image.png", "rb") as image_file: ...
🌐
Readthedocs
pyperclip.readthedocs.io › en › latest
Welcome to Pyperclip’s documentation! — Pyperclip 1.5 documentation
In order to work equally well on Windows, Mac, and Linux, Pyperclip uses various mechanisms to do this. Currently, this error should only appear on Linux (not Windows or Mac). You can fix this by installing one of the copy/paste mechanisms:
Find elsewhere
🌐
Pybites
pybit.es › articles › pyperclip
Copy and Paste with Pyperclip – Pybites
January 6, 2017 - Pyperclip is a module you can import that allows you to copy and paste to and from the clipboard on your computer. It does this through the use of two functions: copy() and paste()
🌐
Note.nkmk.me
note.nkmk.me › home › python › pillow
Get the image from the clipboard with Python, Pillow | note.nkmk.me
April 22, 2022 - ImageGrab.grab() — Pillow (PIL Fork) 9.1.0 documentation · You can also work with the clipboard with pyperclip. Copy and paste text to the clipboard with pyperclip in Python
🌐
PyPI
pypi.org › project › pyperclip
pyperclip · PyPI
>>> import pyperclip >>> pyperclip.copy('The text to be copied to the clipboard.') >>> pyperclip.paste() 'The text to be copied to the clipboard.'
      » pip install pyperclip
    
Published   Sep 26, 2025
Version   1.11.0
🌐
Python Pool
pythonpool.com › home › blog › a simple guide to python pyperclip module
A Simple Guide to Python Pyperclip Module - Python Pool
March 25, 2022 - No, since the pyperclip only supports plain text(int, str, boolean, float), hence, copying images isn’t possible.
🌐
GeeksforGeeks
geeksforgeeks.org › pyperclip-module-in-python
Pyperclip module in Python - GeeksforGeeks
February 27, 2020 - # importing the library import pyperclip as pc text1 = "GeeksforGeeks" # copying text to clipboard pc.copy(text1) # pasting the text from clipboard text2 = pc.paste() print(text2) Output :
🌐
GitHub
github.com › asweigart › pyperclip › issues › 72
Can this lib has ability to copy image? · Issue #72 · asweigart/pyperclip
August 4, 2016 - asweigart / pyperclip Public · Notifications · You must be signed in to change notification settings · Fork 211 · Star 1.8k · New issueCopy link · New issueCopy link · Closed · Closed · Can this lib has ability to copy image?#72 · Copy link · codeskyblue ·
Author   codeskyblue
🌐
TutorialsPoint
tutorialspoint.com › copy-and-paste-to-your-clipboard-using-the-pyperclip-module-in-python
Copy and paste to your clipboard using the pyperclip module in Python
February 11, 2021 - Introduction We will be using the pyperclip module in order to copy and paste content to the clipboard. It is cross−platform and works on both Python 2 and Python 3. Copying and pasting from and to the
🌐
Tutorialspoint
tutorialspoint.com › python › python_pyperclip_module.htm
Python - pyperclip Module
The pyperclip module is very important to automate the text processing which involves the clipboard. You can copy data from your script and place it directly to the clipboard and then be easily pasted in any document, browser or any application.
🌐
Stack Overflow
stackoverflow.com › questions › tagged › pyperclip
Newest 'pyperclip' Questions - Stack Overflow
Look, I'm wondering what C function the pyperclip library in python is based on. I couldn't find this information so I decided to ask here. And as I understand, these fundamental functions differ on ... ... I need to copy data in clipboard through loop. I did this, i added sleep for 0.25s, and it works i guess.
🌐
YouTube
m.youtube.com › watch
Get Image From Clipboard In Python.
Share your videos with friends, family, and the world
Published   May 8, 2021
🌐
YouTube
youtube.com › watch
Clipboard copy in python using Pyperclip and pandas - YouTube
In this video, you will learn how to copy data to clipboard in Python using the pyperclip module and pandas library.source code : https://theprogrammingporta...
Published   November 7, 2020