开发者

save the image in the clipboatd - in Python/Tkinter

开发者 https://www.devze.com 2023-03-23 05:58 出处:网络
the subject says it all: is it po开发者_开发问答ssible to take an image present in the clipboard and save it to file under Tkinter?Here is a script that should get let you get arbitrary clipboard data

the subject says it all: is it po开发者_开发问答ssible to take an image present in the clipboard and save it to file under Tkinter?


Here is a script that should get let you get arbitrary clipboard data on windows.

import win32clipboard as clip

# The standard windows clipboard formats
formats = ['CF_OEMTEXT', 'CF_PALETTE', 'CF_TEXT', 'CF_ENHMETAFILE', 'CF_UNICODETEXT', 
            'CF_BITMAP', 'CF_METAFILEPICT', 'CF_DIB', 'CF_DIBV5']

def getFromClipboard(format):
    '""Returns a given type of data from the clipboard.'
    data = None
    clip.OpenClipboard(0)
    if clip.IsClipboardFormatAvailable(format):
        data = clip.GetClipboardData(format)
    clip.CloseClipboard()
    return data

good_formats = []
clip.OpenClipboard(0)
for format in formats:
    if clip.IsClipboardFormatAvailable(format):
        good_formats.append(format)
clip.CloseClipboard()

# choose among the good formats here
print good_formats

# use the one you picked here
data = getFromClipboard(good_formats[0])

Then data will be the raw image data and you can just save it to a file normally.

http://msdn.microsoft.com/en-us/library/ms649013%28v=VS.85%29.aspx

http://docs.activestate.com/activepython/2.4/pywin32/win32clipboard__GetClipboardData_meth.html

Provide some information, more is out there.


Well tkinter uses the PIL for most of its advanced image stuff, in which case this problem is quite simple: Just use Image.frombuffer(mode, size, data) with the bytebuffer representing the image and then save it ala im.save(filename)- if you want a special format you can also specify it.

If you want to do it without the PIL, I don't think tkinter offers that kind of functionality out of the box, its image support is extremely limited in itself.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号