Saving an image to file from windows clipboard

Fredrik Lundh fredrik at pythonware.com
Fri Oct 7 15:51:16 EDT 2005


stephen at theboulets.net wrote:

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






More information about the Python-list mailing list