Saving an image to file from windows clipboard

S

stephen

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
 
F

Fredrik Lundh

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.

well, not in one step, but it can load BMP images and save PNG
images. and in this case, grabclipboard should return something
that can be saved directly to a PNG file.
You can of course use the os.path module instead of the path module.
I keep it in my windows start menu for convenience.

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()

a plain

myimage = ImageGrab.grabclipboard()
myimage.save('D:/Profiles/csb046.DS/Desktop/screen.bmp')

should work (if it doesn't, let me know what exception you get).
Thanks PIL folks!

our pleasure.

</F>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top