Add an image to markdown file (Github and local computer) and solve the problem that the image can't be displayed in the md file preview

1. Github[^1]

Chinese version: Zhihu

1. Github[1]

1.1. Locate in the same folder

If the image is at the same folder with the md file, for example, "username/example.md" and "username/1.png", then you should just input:

![](1.png)


1.2. Locate in different places

If the image locates in "username/docs/assets/1.png" and the md file locates in "username/docs/example.md"then you should input :

![](/docs/assets/1.png)


1.3. URL

If the url of the image is "https://example/1.jpg", then you should input:

![](https://example/1.jpg)


2. Local computer

Markdown editor: VS Code

Extention: Markdown Preview Enhanced by Yiyi Wang


2.1. Locate in the same folder

If the image is at the same folder with the md file, for example, "C:\Users\X\Desktop\example.md" and "C:\Users\X\Desktop\1.png", then you should just input:

![](1.png)


2.2. Md file locates in one folder and the image locates in its subfolder

If the image is at a subfolder under the folder where the md file locates, for example, md file locates at "C:\Users\X\Desktop\example.md" and the image locates "C:\Users\X\Desktop\NoExample\1.jpg", then you should input:

![](NoExample/1.jpg)

Note that there can't be spaces in the name of the folder. If there are spaces, replace them with %20. (You can check this when the other codes in this article don't work.)


2.3. URL

It's same as 1.3.


2.4. Absolute file path

If the file path of the image is "C:\Users\X\Desktop\1.png", then input:

![](C:/Users\X\Desktop\1.png)

Note to change the first "\" to "/" so that the image can be shown normally.


2.5. Base64 string[2]

Convert the image to base64 string in below websites:

Image to Base64 converter

Image to Base64 converter to convert Image to Base64 String

then you should input:

![](data:image/png;base64,...(the base64 string))

or you can input:

![][tmp]

at the place where you want to insert image and input:

[tmp]: data:image/png;base64,... (the base64 string)

at the end of the article to place the base64 string at the end of the article. (png as an example)

Note it is [tmp] rather than (tmp), [] and not (), and you should input "data:image/png;base64," before your plain base64 string. No space between them.

If it you still can't see the image in preview, save your md file to local then have a try again. (You can try this when the other codes in 2. don't work.)

This method will embed the image into the md file, so the file will be bigger.


Reference


  1. GitHub Markdown: Add an Image to README.md File ↩︎

  2. 将图片内嵌入Markdown文档中 ↩︎

Comments

Popular posts from this blog

Do math typesetting in one markdown file using KaTeX

Zhihu Scraper: Scraping the text content in Zhihu's public collection folder (Videos and pictures are saved in the form of links)

FFmpeg: Solve the problem that the video file can't be played due to the lack of codec, get the parameters required by the delogo command of removing the watermark, and some other commonly used commands