Grab Image from Clipboard in Python with Pillow

Advertisement

Advertisement

Overview

Introduction

Copy and paste operations are most often associated with text, but you can also copy and paste images and other formats. This example demonstrates how to pull an image off of the system clipboard using the Pillow package in Python. It has a convenient method for pulling images from the clipboard. There are a few ways you might get an image in your clipboard. For example, if you used your Grab or Snipping Tool to capture a section of your screen, but you haven't actually saved it yet, you can CTRL-C to copy the image to your clipboard. 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. Note this is aimed primarily at Windows. These features are only partially or completely unsupported on other platforms.

For text only clipboard operations, check out pyperclip - Cross-platform text only Python 2&3 module.

Install Pillow package

Install the pillow package using pip in your command line shell.

pip install pillow

Grab image from clipboard

This example demonstrates how to pull an image from the clipboard. It will store it in memory and a Pillow image object. Copy an image to your clipboard with CTRL-C before running to ensure an image is actually in your clipboard. After calling grabClipboard() it will clear the clipboard.

This uses the ImageGrab module. Check out the ImageGrab documentation for more details. Not only can it grab an image from the clipboard, it can actually grab directly from the screen taking full or partial screenshots!

# pip install pillow
from PIL import ImageGrab

img = ImageGrab.grabclipboard()
# or ImageGrab.grab() to grab the whole screen!

print(img)
# <PIL.BmpImagePlugin.DibImageFile image mode=RGB size=380x173 at 0x16A43064DA0>

Save image to disk

This example will pull an image from your clipboard and save it as both a PNG and a JPEG.

If you get an error like

AttributeError: 'NoneType' object has no attribute 'save'
it means there was no image on the clipboard when it tried to pull. Make sure to copy an image to the system clipboard with CTRL-C or some other method.

from PIL import ImageGrab

img = ImageGrab.grabclipboard()
       
# Save the image to disk
img.save('paste.png', 'PNG')
img.save('paste.jpg', 'JPEG')

Get image bytes

You might need the raw image bytes instead of the fancy Image object that pillow has. You can get the bytes by simply using the same save() method used to write files. Instead of passing it a filename though, pass it an in-memory binary stream, BytesIO object.

For more information on working with bytes and binary data, check out my tutorial, Working with Binary Data in Python.

from PIL import ImageGrab
import io

img = ImageGrab.grabclipboard()

# Store the bytes in a byte stream
img_bytes = io.BytesIO()
img.save(img_bytes, format='PNG')

print(img_bytes.getvalue())

Convert bytes to base64 HTML img tag

If you want to create a single standalone HTML file with all the images included, it can be useful to convert an image to base64 encoding and embed it in your document. If you've only ever seen img tags that use the src attribute to link to an external file, this is what the base64 embedded version looks like:

<!-- Base64 image example -->
<img src="data:image/png;base64, AAAnr4nykAwk9WqzMg==" />

This next example demonstrates how to pull an image from the clipboard, and then convert it to base64 and print out a valid HTML img tag with the image contents embedded. Keep in mind that base64 encoded images do take up slightly more base than the binary equivalent, but they are safe to transfer over text-only mediums that do not support binary.

from PIL import ImageGrab
import io
import codecs

# Pull image from clibpoard
img = ImageGrab.grabclipboard()

# Get raw bytes
img_bytes = io.BytesIO()
img.save(img_bytes, format='PNG')

# Convert bytes to base64
base64_data = codecs.encode(img_bytes.getvalue(), 'base64')

# Convert base64 data to a string
base64_text = codecs.decode(base64_data, 'ascii')

# Create an HTML img tag with data embedded
html_img_tag = "<img src="data:image/png;base64, %s" />" % base64_text

# Print HTML tag to console (might be long).
# You can put this img tag in to HTML and the browser
# will render the image.
print(html_img_tag)

Conclusion

After reading this you should be able to pull images off of your system clipboard and save them to disk, manipulate the bytes in memory, and convert the image to base64 text.

Reference links

Advertisement

Advertisement