【发布时间】:2021-03-28 19:18:41
【问题描述】:
我正在尝试实现与 Firefox 和 Chrome 等现代网络浏览器相同的功能。当您右键单击网络上的透明图像,然后选择“复制图像”时,图像就会被复制到剪贴板。因此,您可以稍后将其粘贴到 Discord 聊天中。 并且保留了透明度。
我想在 Python 3 中做同样的事情。我希望能够使用 python 脚本将存储在我计算机上的图像(例如以 .png 格式)复制到 Windows 剪贴板,然后将其粘贴到 Discord 聊天中并具有透明度保存。
尝试 #1
我发现this 帖子包含以下代码。但正如我在代码中看到的,图像仅转换为 RGB,并且 alpha 通道丢失了。
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()
image = Image.open("test.png")
output = BytesIO()
image.convert("RGB").save(output, "BMP")
data = output.getvalue()[14:]
output.close()
send_to_clipboard(win32clipboard.CF_DIB, data)
我还尝试将图像保存为 png,如下所示:image.save(output, "PNG").
但这不起作用,当我尝试将图像粘贴到聊天时,discord 崩溃了。
尝试 #2
接下来我尝试使用CF_HDROP,希望discord桌面应用能够识别它。
import ctypes
import pythoncom
import win32clipboard
from ctypes import wintypes
class DROPFILES(ctypes.Structure):
_fields_ = [("pFiles", wintypes.DWORD),
("pt", wintypes.POINT),
("fNC", wintypes.BOOL),
("fWide", wintypes.BOOL)]
path = r"D:\Visual Studio Code Projects\clipboard-test\test.png"
offset = ctypes.sizeof(DROPFILES)
size = offset + (len(path) + 1) * ctypes.sizeof(ctypes.c_wchar) + 1
buffer = (ctypes.c_char * size)()
df = DROPFILES.from_buffer(buffer)
df.pFiles = offset
df.fWide = True
wchars = (ctypes.c_wchar * (len(path) + 1)).from_buffer(buffer, offset)
wchars.value = path
stg = pythoncom.STGMEDIUM()
stg.set(pythoncom.TYMED_HGLOBAL, buffer)
win32clipboard.OpenClipboard()
try:
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardData(win32clipboard.CF_HDROP, stg.data)
finally:
win32clipboard.CloseClipboard()
Windows 文件资源管理器能够识别我存储在剪贴板中的数据,但 discord 不能。所以那里没有可见的结果。
尝试 #3
我终于找到了CF_HTML or html clipboard format。
import win32clipboard
class HTMLClipboard:
def __init__(self):
win32clipboard.OpenClipboard()
self.format = win32clipboard.RegisterClipboardFormat("HTML Format")
self.headers = "Version:0.9\r\n" +\
"StartHTML:00000000\r\n" +\
"EndHTML:00000000\r\n" +\
"StartFragment:00000000\r\n" +\
"EndFragment:00000000\r\n"
def _insertHeaders(self, data):
data = self.headers + data
hStartHtml = data.find("StartHTML")
startHtml = str(data.find("<html>"))
data = data[:hStartHtml + 18 - len(startHtml)] + startHtml + data[hStartHtml + 19:]
hEndHtml = data.find("EndHTML")
endHtml = str(len(data) - 1)
data = data[:hEndHtml + 16 - len(endHtml)] + endHtml + data[hEndHtml + 17:]
hStartFragment = data.find("StartFragment")
startFragment = str(data.find("<!--StartFragment-->") + 20)
data = data[:hStartFragment + 22 - len(startFragment)] + startFragment + data[hStartFragment + 23:]
hEndFragment = data.find("EndFragment")
endFragment = str(data.find("<!--EndFragment-->") + 1)
data = data[:hEndFragment + 20 - len(endFragment)] + endFragment + data[hEndFragment + 21:]
return data
def write(self, html):
data = "<html>\r\n" +\
"<body>\r\n" +\
"<!--StartFragment-->" +\
html + "" +\
"<!--EndFragment-->\r\n" +\
"</body>\r\n" +\
"</html>"
data = self._insertHeaders(data)
win32clipboard.SetClipboardData(self.format, data.encode("ascii"))
def read(self):
pass
def close(self):
win32clipboard.CloseClipboard()
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
self.close()
with HTMLClipboard() as clip:
clip.write("<img class='lnXdpd' alt='Google' src='https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png' srcset='/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png 1x, /images/branding/googlelogo/2x/googlelogo_color_272x92dp.png 2x' data-atf='1' width='272' height='92'>")
Discord 再次显示没有可见的结果。但奇怪的事情发生了,我使用 Microsoft Edge(基于 Chromium 的新版本)从网络上复制图像到剪贴板,并尝试使用我的代码仅重写 html 格式部分,它仍然有效。
所以我猜要么我还是忘记了一些东西,一些我没有设置但浏览器设置的剪贴板格式,或者 discord 根本不使用 html 剪贴板格式从剪贴板导入图像。
我什至尝试同时使用上述尝试中的所有剪贴板格式,但除了透明度丢失(黑色背景)之外没有可见结果。
我真的不知道网络浏览器是如何做到的。任何帮助将不胜感激。
【问题讨论】:
标签: python python-3.x windows clipboard transparency