Permanently embed bs64 pictures in Markdown, convert the pictures to bs64 after compression by python PIL and other powers, and directly compress 2M into 100k

Introduction

Sometimes when I write technical documents or notes by myself, I find that the inserted pictures under the md syntax are either local or image beds.

![img](./lady.jpg)
![img](http://......)

The disadvantage of local is slow loading on the server (gitgub), or forgetting to post pictures when sending documents to others. . .
I
have seen pictures directly embedded in the document before. Of course, the disadvantage is that it will increase the file size, but if the picture is lost,
some people will say, a The bs64 code converted from a few megabytes of pictures is longer than my article (I don’t believe you, try copying the bs64 below to see how many characters, Xiaoxin crashes when copying -.-), is there any way? ?
There must be a way
to number the pictures, and put all the pictures base64 encoding at the end of the document. The format is as follows:
where the picture is inserted: ![图片缺失描述][图片编号]
at the end of the document: [图片编号]:data:image/jpg;base64,base64编码
but there is another disadvantage that Typora will get stuck. Said it could not be opened. . Actually less than 4M
Insert picture description here
! ! ! This is a headache for me, so I thought about it for a while. The image that was not compressed before and stored in the md file is still relatively large, so I studied py compression.

GIthub project address

effect

Insert picture description here

format! ! !

Where the picture is inserted: ![图片缺失描述][图片编号]
at the end of the document:[图片编号]:data:image/jpg;base64,base64编码
Insert picture description here

Online conversion

Bing search pictures directly to bs64,
such as here

py convert bs64

import base64
f=open('lady.jpg','rb') #图文件lady.jpg,自行更改
pbs64=base64.b64encode(f.read()) #读取文件内容,转换为base64编码
f.close()
print(pbs64)

In this way, expanding the bs64 code py will be stuck (2k lines...), so I make a slight improvement, and write the generated large byte stream into a text file (bs64.txt), and open it with Notepad. Dead, the notepad stuck for two seconds in the actual test, editplus is much better, there is basically no such problem, and bs64 in editplus does not automatically wrap by default, it is very comfortable to copy a line directly

import base64
f=open('lady.jpg','rb') #图文件lady.jpg,自行更改
pbs64=base64.b64encode(f.read()) #读取文件内容,转换为base64编
f.close()
#print(pbs64)
mf=open("bs64.txt","wb") #新建目标文件存储bs64码
mf.write(pbs64)
mf.close

You ask me why I don’t put an example here? ? ? Because CSDN can't bear to copy the bs64 code. . . . If you don’t believe
me , look at the end of the article, [I originally copied it here to embed bs64, but it stuck. . Can not save or publish articles]
example look at my blog site it
http://kearney.club/2020/06/11/%E6%88%91%E7%9A%84%E8%97%8F%E5%B0% B8%E4%B8%8D%E8%A7%81%E4%BA%86/
This is static hosting on github Page, which is displayed normally. Actually, my article is more than a thousand words in the editing process of Typora, and three pictures are added The bs64 becomes 3w multi-word. . . I looked at the background and the memory occupied is not large, but it may be the problem of the amount of data loaded by typora itself. Copying and pasting in the 3w multi-character is a bit lagging (I have more than 60% of the free 32G memory).

lossless compression

How to use: Pull directly to the bottom of the code and modify the picturepath. The
picture and code need to be in the same directory

省略的代码放在了github上。。省的大家看得烦
https://github.com/BackMountainDevil/image-into-bs64

def img_bs64(infile,outfile='', savefile =''):
    compress_image(infile)
    outfile = get_outfile(infile, outfile)
    f=open(outfile,'rb')
    pbs64=base64.b64encode(f.read()) 
    f.close()
    dir, suffix = os.path.splitext(infile)
    savefile = '{}-out{}'.format(dir, '.txt')
    mf=open(savefile,"wb")
    mf.write(pbs64)
    mf.close
    os.remove(outfile)
    
    
if __name__ == '__main__':
    picturepath= "luffy.jpg" 
    img_bs64(picturepath)

The code is all cv. Do you really leave without a thumbs up?? It’s OK to click a Star
at Github

reference

  • https://github.com/BackMountainDevil/image-into-bs64
  • https://cn.bing.com/search?q=%e5%9b%be%e7%89%87%e8%bd%acbs64&qs=SC&pq=%e5%9b%be%e7%89%87%e8%bd%acbs&sc=1-5&cvid=5B916F1601B6423BBAC2240FC489E031&FORM=QBRE&sp=1
  • https://oktools.net/image2base64
  • https://blog.csdn.net/baidu_31492511/article/details/90704123
  • https://blog.csdn.net/cxs123678/article/details/82533306
  • https://www.jianshu.com/p/280c6a6f2594
  • https://www.runoob.com/python/python-files-io.html
  • https://blog.csdn.net/baidu_33221362/article/details/81566874
  • https://blog.csdn.net/qq_31567335/article/details/82322858?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.add_param_isCf&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.add_param_isCf
  • https://www.cnblogs.com/li1992/p/10675769.html
  • https://blog.csdn.net/julac/article/details/105492336
  • https://github.com/BackMountainDevil/image-into-bs64
  • https://blog.csdn.net/qq_36387683/article/details/100579736
![picture][1]
其它正文
末尾
[1]:data:image/jpg;base64,/9j/4AAQSkZJRgABAQAASABIAAD/4QBYRXhpZgAATU0AKgAAAAgAAgESAAMAAAABAAEAAIdpAAQAAAABAAAAJgAAAAAAA6ABAAMAAAABAAEAAKACAAQAAAABAAAEOKADAAQAAAABAAAFoAAAAAD/7QA4UGhvdG9zaG9wIDMuMAA4QklNBAQAAAAAAAA4QklNBCUAAAAAABDUHYzZjwCyBOmACZjs+EJ+/8IAEQgFoAQ4AwEiAAIRAQMRAf/EAB8AAAEFAQEBAQEBAAAAAAAAAAMCBAEFAAYHCAkKC//EAMMQAAEDAwIEAwQGBAcGBAgGcwECAAMRBBIhBTETIhAGQVEyFGFxIweBIJFCFaFSM7EkYjAWwXLRQ5I0ggjhU0AlYxc18JNzolBEsoPxJlQ2ZJR0wmDShKMYcOInRTdls1V1pJXDhfLTRnaA40dWZrQJChkaKCkqODk6SElKV1hZWmdoaWp3eHl6hoeIiYqQlpeYmZqgpaanqKmqsLW2t7i5usDExc

Guess you like

Origin blog.csdn.net/weixin_43031092/article/details/106672965