【问题标题】:save the image in the clipboatd - in Python/Tkinter将图像保存在 clipboatd 中 - 在 Python/Tkinter 中
【发布时间】:2011-07-25 14:23:03
【问题描述】:

主题说明了一切:是否可以拍摄剪贴板中的图像并将其保存到 Tkinter 下的文件中?

【问题讨论】:

    标签: python tkinter


    【解决方案1】:

    这是一个脚本,可以让您在 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])
    

    那么data就是原始图像数据,你可以正常保存到文件中。

    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

    提供一些信息,还有更多信息。

    【讨论】:

    • 哎呀,我需要它来补充操作系统:Linux 和 OSX!
    • 我没有看到任何在 posix 系统上获取非文本剪贴板数据的方法。
    • 不——听起来它会起作用,但我不知道如何获得尺寸或类似的东西。
    • @noob 奇怪:第一个链接不是那么多,但我对第二个有很多希望:我的 prog 使用 ImageMagick 很多。不幸的是,我找不到说服import 使用剪贴板作为输入的方法
    【解决方案2】:

    好吧,tkinter 使用 PIL 来处理其大多数高级图像内容,在这种情况下,这个问题非常简单:只需将 Image.frombuffer(mode, size, data) 与表示图像的字节缓冲区一起使用,然后将其保存为 im.save(filename)- 如果你想要一个特殊的格式也可以指定。

    如果你想在没有 PIL 的情况下这样做,我不认为 tkinter 提供了开箱即用的那种功能,它的图像支持本身就非常有限。

    【讨论】:

    • 好像是这个!但是我尝试使用它没有运气wx,wy = 3000,3000 buf = "a"*wx*wy im=Image.frombuffer("L", (wx, wy), buf, "raw", "L", 0, 1) ...我的问题是我不知道如何提前知道剪贴板中图像的图像大小(也不是模式)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-17
    • 2017-05-26
    • 1970-01-01
    • 1970-01-01
    • 2021-06-14
    相关资源
    最近更新 更多