Jump to content

Clipboard image to data-uri

Recommended Posts

I often have to generate data-uri for some bitmap data.


Previously I had to save the data to a file and process it that way.

I needed to support

- data currently in clipboard


I can get old data from clipboard history and make it current clipboard


My workaround is using `pngpaste` terminal command and a workflow that does:

#!/usr/bin/env bash
echo "data:image/png;base64,$(pngpaste -b)" | pbcopy


https://www.gingerbeardman.com/alfred/image to data-uri.alfredworkflow


That works well.


I'm wondering if there is a more efficient way to do this in Alfred that relies only in built-in tools?



Link to comment

Here’s how you can save image data from the clipboard via AppleScript, with quite a bit of error handling. This particular code saves it to the Desktop, named as a screenshot. WebScreenshot can do it too.


  set imageData to the clipboard as {«class PNGf»}
on error
  tell me to error "Clipboard data does not seem to be an image"
end try

set filePath to (POSIX path of (path to desktop folder)) & (do shell script "date +'Screenshot %Y-%m-%d at %H.%M.%S.png'")

tell application "System Events" to if filePath exists then tell me to error "Path exists: " & filePath

set imageFile to open for access filePath with write permission
write imageData to imageFile
close access imageFile


Alternatively, look inside ~/Library/Application Support/Alfred/Databases/clipboard.alfdb.data which has the images stored as tiffs. You can convert them to PNG with the Convert Image Format Automation Task.

Link to comment

Thanks, not quite what I was looking for.


I need the data-uri (base64) of the image, and the process needs to be transient - I don't want files to be created on disk.


This is what my workflow does (produces base64 of the current clipboard image) but it relies on the third party tool pngpaste 

Link to comment

Right. I may still be able to help but to give the best answer I need to understand what’s the scope of the request. You want to get the base64 of the image without any dependencies on software which does not ship by default with macOS, correct? That is simpler if you create a temporary image then run the base64 command on it.

I’ll simplify the code above and show what I mean. Note how it creates the image in the tmp directory.


set imageData to the clipboard asclass PNGf»}

set filePath to (POSIX file "/tmp/base64image.png")


set imageFile to open for access filePath with write permission

write imageData to imageFile

close access imageFile


Then you connect that to a Zsh Run Script:

echo "data:image/png;base64,$(base64 --input=/tmp/base64image.png)" | pbcopy
rm '/tmp/base64image.png'

You don’t even need to rm the image if you don’t want, as /tmp is cleaned up on reboots and the AppleScript overwrites the path.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...