Write image to Windows clipboard in python with PIL and win32clipboard?

前端 未结 2 2013
太阳男子
太阳男子 2020-11-30 13:15

I\'m trying to open an image file and copy the image to the Windows clipboard. Is there a way to fix this:

import win32clipboard
from PIL import Image

def          


        
相关标签:
2条回答
  • 2020-11-30 13:32
    from cStringIO import StringIO
    import win32clipboard
    from PIL import Image
    
    def send_to_clipboard(clip_type, data):
        win32clipboard.OpenClipboard()
        win32clipboard.EmptyClipboard()
        win32clipboard.SetClipboardData(clip_type, data)
        win32clipboard.CloseClipboard()
    
    filepath = 'image.jpg'
    image = Image.open(filepath)
    
    output = StringIO()
    image.convert("RGB").save(output, "BMP")
    data = output.getvalue()[14:]
    output.close()
    
    send_to_clipboard(win32clipboard.CF_DIB, data)
    
    0 讨论(0)
  • 2020-11-30 13:40

    The file header off-set of BMP is 14 bytes. Well, BMP is also known as the device independent bitmap (DIB) file format, so you don't need to worried about the magic number 14.

    FYI, it does need a windows clipboard API. Hence you can use BMP but can't use

    image.convert("RGB").save(output, "PNG")
    data = output.getvalue()[8:]
    

    even you know the offset is 8 for PNG.

    0 讨论(0)
提交回复
热议问题