markdown想内嵌图片只能使用base64格式。这个脚本读取剪贴板中内容,转换为相应格式之后写回到剪贴板,方便我个人QQ截图之后直接粘贴进md里面。
- 使用方法:将png图片放到脚本同目录下,或截图之后运行该脚本。建议自己写一个bat。图片不能太大。
- 各种md编辑器对图片大小的支持程度不一样,我怀疑是和单行文本上限有一定关系。反正脚本是没错的,至于你的编辑器能不能看就是另一回事了。
import base64
import os
from PIL import ImageGrab
import pyperclip
import sys
dir = r'C:\Users\TOKAMAK\Desktop\study\base' #指定文件夹的路径
flag=0
def convert(path):
f=open(path,'rb') #二进制方式打开图文件
ls_f=base64.b64encode(f.read()) #读取文件内容,转换为base64编码
f.close()
ls_f=str(ls_f,"utf-8")
content="<img src=\"data:image/png;base64,"+ls_f+'">'
pyperclip.copy(content)
spam = pyperclip.paste()
os.remove(path)
for root, dirs, files in os.walk(dir): #遍历该文件夹
for file in files: #遍历刚获得的文件名files
(filename, extension) = os.path.splitext(file) #将文件名拆分为文件名与后缀
if (extension == '.png'):
path=dir+"\\"+file
flag=1
convert(path)
break
if(flag==1):
try:
sys.exit(0)
except:
pass
else:
im = ImageGrab.grabclipboard()
im.save(dir+"\\p.png",'PNG')
path=dir+"\\p.png"
convert(path)
坚果之云 Markdown