Markdown reference picture, and does not use the solution online links

First introduced three methods used under markdown pictures

  • Use local picture, the disadvantage is to use a local absolute path, not suitable for the document to make the migration, otherwise there will be situations image link failure
![thisisimage](C:\\Users\\Goose\\Desktop\\cc1.jpg)
  • Use url link to insert a picture, the disadvantage is that this method relies on a network. For example, you need to upload some pictures custom, you must first upload pictures to the network to use, while also facing picture link failure, the same picture can not view the situation
![thisisimage](https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1560707333204&di=2e7438b797daaddc837aa5659628b5c2&imgtype=0&src=http%3A%2F%2Fimage.namedq.com%2Fuploads%2F20190105%2F23%2F1546702651-RprCqAefWl.jpg '你瞅啥') # 代码效果见最下方
  • Use base64 picture into a string md file can be identified to facilitate the migration and do not upload the network, but the disadvantage is the character after the conversion really long ...
![thisisimage](data:image/png;base64,/9j/4AAQSkZJRgABAQAAAQABAAD...) # 字符串太长,后面省略,如下等同

In addition, taking into account the reading experience, the picture can be transformed into an overly long string Finally, in the following format

![thisisimage][tthisisimagesisimage]
...
...
[tthisisimagesisimage]:data:image/png;base64,/9j/4AAQSkZJRgABAQAAAQABAAD...

The following code is used to convert the image to a long string markdown used, because the longer the greater the additional image, converted into a string, so increasing the scaled image functions

#!/bin/env python
import base64
from PIL import Image # pip install pillow

max_x = 450
image_infile = 'C:\\Users\\Goose\\Desktop\\cc.jpg'
image_outfile = 'C:\\Users\\Goose\\Desktop\\cc1.jpg' # 输出和输入格式的文件类型必须相同,比如都是jpg或png等
img = Image.open(image_infile)
x, y = img.size
print(x, y)

x_scale = x / max_x
print(x_scale)
new_x = int(x / x_scale)
new_y = int(y / x_scale)

print(new_x, new_y)
out = img.resize((new_x, new_y), Image.ANTIALIAS)
out.save(image_outfile)

with open(image_outfile, 'rb') as f:
    ls_f = base64.b64encode(f.read())
print(ls_f)

The following code is converted into the image string, then converted to a picture

#!/bin/env python
import base64

img_str = '/9j/4AAQSkZJRgABAQAAAQABAAD...'
img_outfile = 'C:\\Users\\Goose\\Desktop\\cc2.jpg'
img_data = base64.b64decode(img_str)
with open(img_outfile, 'wb') as f:
    f.write(img_data)

Web links pictures for example as follows

thisisimage

String conversion processing and image reduction follows

thisisimage

Guess you like

Origin www.cnblogs.com/youbiyoufang/p/11032652.html