Skip to content

@GuyCarver /

Embed URL


Subversion checkout URL

You can clone with
Download ZIP
Pythonista image2base64
#covert an image in the clipboard to a 57x57 rgb icon and store base64 version of it into the clipboard.
#if an image is not in the clipboard the base64 string 'b64str' will be loaded and displayed.
#after running the 1st time replace the contents of b64str with the clipboard.
from PIL import Image
import clipboard
from StringIO import *
import base64
replace this with contents of clipboard after 1st run.
def main():
global b64str
cpimage = clipboard.get_image()
#if image was in clipboard.
if cpimage:
#resize to icon size.
icon = cpimage.resize((57, 57), Image.BILINEAR)
#convert to rgb format.
icon = icon.convert('RGB')
# #show resized image.
#create string buffer to write png file to.
iconstr = StringIO()
#write image to string buffer in png format., 'png')
#convert save buffer to base64.
b64str = base64.standard_b64encode(iconstr.getvalue())
#put base64 string in clipboard.
#now decode to test.
mystr = base64.standard_b64decode(b64str)
#read file from string buffer.
stb = StringIO(mystr)
img =
#show the image.
#print some info.
print str(img.format)
print str(img.mode)
print str(img.size)
print str(
if __name__ == '__main__':

It did not work for Mac though

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.