Saving an image to file from windows clipboard

stephen at theboulets.net stephen at theboulets.net
Fri Oct 7 15:29:23 EDT 2005


I just threw this together because I find myself needing it now and
then. Requires PIL and optionally ImageMagick to convert to png, since
I think PIL is not able yet to convert a bmp to a png. You can of
course use the os.path module instead of the path module. I keep it in
my windows start menu for convenience. Thanks PIL folks!

from PIL import ImageGrab
from os import popen
from path import path

myimage = ImageGrab.grabclipboard()

myfile = path(r'D:\Profiles\csb046.DS\Desktop\screen.bmp')
f = file(myfile,'wb')
myimage.save(f)
f.close()

command = 'convert "' + myfile + '" "' + myfile.replace('bmp','png') +
'"'

popen(command).read()
myfile.remove()

Stephen




More information about the Python-list mailing list