简体   繁体   中英

having issue copy image file to clipboard and manually paste it in browser, python

I am trying to copy image file to clipboard, and then I can manually type 'ctrl + v' in window and paste image in broswer such as paste it in email body. It seems no error when run the code, but went paste it, my chrome close immediately. Here is my code:

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 = r'C:\Users\erica\Desktop\Kai\logo - innoID\image.jpg'
image = Image.open(filepath)

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

send_to_clipboard(win32clipboard.CF_DIB, data)

My computer test your code and chrome close immediately too,I guess it should be a picture format problem in memory, maybe you can modify JPG to BMP format, I tested and it's useful, I am not familiar with the underlying memory operation of the code, I hope to help you a little.

    output = BytesIO()
    image.convert("RGB").save(output, "BMP")
    data = output.getvalue()[14:]
    output.close()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM