将文本写入剪切板
import pyperclip
pyperclip.copy('北巷的猫')
从剪切板读取文本
import pandas as pd
df = pd.read_clipboard()
df
import pyperclip
text = pyperclip.paste()
text
'北巷的猫'
从剪切板读取图像
from PIL import Image, ImageGrab
im = ImageGrab.grabclipboard()
im

import io
buf = io.BytesIO()
im.save(buf, format='png')
byte_im = buf.getvalue()
len(byte_im)
1850189
从文件读取图像
file = r'C:\Users\BXDM\Pictures\O1CN01Xg2hFN2D9HRpvXlKH_!!2208169218566.jpg_400x400.jpg'
from PIL import Image
img = open(file, 'rb').read()
print(len(img))
display(Image.open(file))
40114

参考资料
python从粘贴板上读取数据 Python从剪贴板上复制图像到PIL库中 Convert PIL or OpenCV Image to Bytes without Saving to Disk
|