Last active
August 31, 2018 12:38
-
-
Save zombie110year/640cf8732446065f856eb00499abfa3c to your computer and use it in GitHub Desktop.
从剪贴板读取复制的图片路径, 转换图片为 URL data scheme 格式的 base64 编码并存入剪贴板.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! /usr/bin/env python3 | |
| # -*- coding: utf-8 -*- | |
| from base64 import b64encode # 将图片编码为 Base64 | |
| import win32clipboard | |
| PNG_BASE64_TEMP = "data:image/png;base64,{code}" # 插入 data URL scheme 的模板 | |
| """复制文件, 从剪贴板 CF_HDROP 读取路径, 将其以二进制模式打开并编码为base64, 再存入系统剪贴板 CF_TEXT.""" | |
| def get_base64(file_path): | |
| "输入文件路径(字符串), 返回此文件的 base64 字符串" | |
| with open(file_path, "rb") as r: | |
| src_input = r.read() | |
| src_output = b64encode(src_input) | |
| str_output = src_output.decode("ascii") | |
| return str_output | |
| def put_clipboard(str_input): | |
| "将传入的字符串放入剪贴板" | |
| win32clipboard.OpenClipboard() | |
| win32clipboard.EmptyClipboard() | |
| win32clipboard.SetClipboardText(str_input.encode("ascii"), win32clipboard.CF_TEXT) | |
| win32clipboard.CloseClipboard() | |
| def full_process(): | |
| """复制目标文件, 再使用此函数""" | |
| win32clipboard.OpenClipboard() | |
| file_input = win32clipboard.GetClipboardData(win32clipboard.CF_HDROP)[0] # 从剪贴板中获取文件路径 | |
| win32clipboard.CloseClipboard() | |
| get_str = get_base64(file_input) | |
| put_str = PNG_BASE64_TEMP.format(code=get_str) | |
| put_clipboard(put_str) | |
| print("任务完成, 编码已存入剪贴板!") | |
| if __name__ == "__main__": | |
| full_process() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment