Copy image to clipboard?

后端 未结 4 604
既然无缘
既然无缘 2020-12-10 06:19

First of all, the question on SO copy image to clipboard in python leads to answer Write image to Windows clipboard in python with PIL and win32clipboard?, which was only go

相关标签:
4条回答
  • 2020-12-10 06:43

    You can copy bitmap image to clipboard using winclip32 install:

    pip install winclip32
    

    copy:

    import winclip32
    winclip32.set_clipboard_data(winclip32.BITMAPINFO_STD_STRUCTURE, your_binary_here)
    
    0 讨论(0)
  • 2020-12-10 06:59

    I did copy the code and replace the StringIO with BytesIO and it worked! (with *.jpg and *.png files) Thank you so much!

    from io import BytesIO
    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 = 'Ico2.png'
    image = Image.open(filepath)
    
    output = BytesIO()
    image.convert("RGB").save(output, "BMP")
    data = output.getvalue()[14:]
    output.close()
    
    send_to_clipboard(win32clipboard.CF_DIB, data)
    
    0 讨论(0)
  • 2020-12-10 07:02

    You don't want StringIO here. Images are raw binary data, and in Py3, str is purely for text, bytes and bytes-like objects (bytearray, contiguous memoryviews, mmaps) are for binary data. To replace Py2's StringIO.StringIO for binary data, you want to use io.BytesIO in Python 3, not io.StringIO.

    0 讨论(0)
  • 2020-12-10 07:04

    For those who want to copy-paste

    # parameter must be a PIL image 
    def send_to_clipboard(image):
        output = BytesIO()
        image.convert('RGB').save(output, 'BMP')
        data = output.getvalue()[14:]
        output.close()
    
        win32clipboard.OpenClipboard()
        win32clipboard.EmptyClipboard()
        win32clipboard.SetClipboardData(win32clipboard.CF_DIB, data)
        win32clipboard.CloseClipboard()
    
    0 讨论(0)
提交回复
热议问题