16

How do you set an image as the clipboard with pbcopy?

This doesn't work:

cat image.png | pbcopy

6 Answers 6

25

Updated Answer

You can actually put a JPEG image in the clipboard using Applescript like this at the command-line:

osascript -e 'set the clipboard to (read (POSIX file "/Users/mark/Desktop/a.jpg") as JPEG picture)'

You can then check what is on the clipboard with:

osascript -e 'clipboard info'

JPEG picture, 175960, «class 8BPS», 641904, GIF picture, 124637, «class jp2 », 102086, TIFF picture, 1481282, «class PNGf», 412940, «class BMP », 1477734, «class TPIC», 609835

And also paste the image into a document, with the usual -V.

Original Answer

You can do this without needing to compile any additional software and just use the tools provided in OS X. Basically, the clipboard is unable to store binary, so you need to uuencode your binary image into simple ASCII data like this:

# Copy image to clipboard
uuencode SomeFile.jpg - | pbcopy

and uudecode when going back the other way

# Paste from clipboard to image file
pbpaste | uudecode -o AnotherFile.jpg
8
  • If you don't want to compile anything this is a great answer. Here is a one line function you can drop into your .bash_profile or .bashrc to use it without remembering the details, usage: impbcopy <filename> . impbcopy() {osascript -e 'set the clipboard to (read (POSIX file '\"$PWD/$1\"') as JPEG picture)'} Apr 4, 2018 at 17:30
  • What about png files? It seems not work when I replace JPEG into PNG.
    – DawnSong
    Jul 15, 2019 at 6:13
  • @DawnSong Maybe try a new question with links forward and backward to this one and we’ll see if anyone knows anything new... Jul 15, 2019 at 7:37
  • Very similar answer to another question here
    – Brad Parks
    Dec 4, 2019 at 15:18
  • 3
    @DawnSong instead of as JPEG picture try as «class PNGf»
    – luckman212
    Oct 12, 2020 at 22:23
13

As stated, this won't work with pbcopy, but you can write a little objective-c program to do this: http://www.alecjacobson.com/weblog/?p=3816 . Then you can issue:

cat image.png | impbcopy -
1
  • 1
    You can also combine this with ImageMagick's convert tool. For example, to copy the first page of a pdf as a raster image: convert input.pdf[0] png:- | impbcopy - Jul 14, 2016 at 16:42
3

From the documentation:

The input is placed in the pasteboard as ASCII data unless it begins with the Encapsulated PostScript (EPS) file header or the Rich Text Format (RTF) file header, in which case it is placed in the pasteboard as one of those data types.

It doesn't sound like image data is supported, so it won't work.

1
  • 1
    Wrong - uuencode<->uudecode allow C&P of any binary file to/frpm the PB
    – user5395338
    May 22, 2020 at 2:23
1

The actual (or final) answer to how to copy a PNG image into the macos' clipboard was hidden in the comments to the Mark's answer. As it is not (that) obvious from Mark's answer and it took me a while to find it I will leave it as an answer here:

osascript -e 'set the clipboard to (read (POSIX file "/Users/mark/Desktop/a.png") as  {«class PNGf»})'
1

one alternative that works for both images and animated gif is:

osascript -e 'tell app "Finder" to set the clipboard to ( POSIX file "<PATH_TO_IMAGE>" )'
0

To make osascript work properly (with any file type), don't use read and use absolute path:

osascript -e 'set the clipboard to (POSIX file "/Full/Path/To/image.png")'

So for a file in current directory (note outer double quotes):

osascript -e "set the clipboard to (POSIX file \"$PWD/image.png\")"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.