如何在GitHub的README.md文件中添加图片?

3286

最近我加入了 GitHub,在那里托管了一些项目。

我需要在 README 文件中包含一些图片,但不知道该如何操作。

我搜索了一下,但得到的都是一些链接,告诉我“将图片托管在网络上,并在 README.md 文件中指定图片路径”。

是否有不依赖第三方网络托管服务的方法?


11
重复的问题,已于2012年在https://dev59.com/3mkw5IYBdhLWcg3wBl-A发布。 - leetNightshade
225
不离题,GitHub 是程序员的工具。 - Lance Roberts
6
可能是 如何在 GitHub 仓库的 README 中添加截图? 的重复问题。 - Marcus
https://stackoverflow.com/a/77319849/4393351 - undefined
46个回答

9

虽然GitHub Markdown也可以添加图片,但我建议您使用HTML IMG标签
Github Markdown

![me](https://github.com/samadpls)

HTML标签

<img src='https://github.com/samadpls'/>

9

只需在README.md文件中将代码写在此处[任何名称](图片路径)

![Screenshot](screenshot1.png)

复制了用户3638471的答案。 - yurloc

9

无需编写任何代码,GitHub上的readme文件现在支持拖放功能。

  1. 打开README.md文件
  2. 点击“编辑此文件”
  3. 拖拽你的图片
  4. 点击“提交更改”

8

我通常会在网站上托管图片,这可以链接到任何托管的图片。只需将此放入自述文件即可。适用于.rst文件,不确定对于.md文件是否适用。

.. image:: https://url/path/to/image
   :height: 100px
   :width: 200 px
   :scale: 50 %

8
你可以在README.md中使用备用的Github CDN链接引用项目中的图片(或外部链接)。
链接的URL将会像这样:
https://cdn.rawgit.com/<USER>/<REPO>/<BRANCH>/<PATH>/<TO>/<FILE>

我在项目中有一个SVG图像,但当我在Python项目文档中引用它时,它无法渲染。
项目链接
这是文件的项目链接(不会以图像形式呈现):

https://github.com/jongracecox/anybadge/blob/master/examples/awesomeness.svg

示例嵌入图像:image

原始链接

这是文件的原始链接(仍然不会呈现为图像):

https://raw.githubusercontent.com/jongracecox/anybadge/master/examples/awesomeness.svg

示例嵌入式图像:image

CDN链接

使用CDN链接,我可以使用以下方式链接到文件(呈现为图像):

https://cdn.rawgit.com/jongracecox/anybadge/master/examples/awesomeness.svg

示例嵌入图像:image

这是我如何在我的README.md文件和PyPi项目的reStructredText文档(此处)中使用来自我的项目的图像的方法。


8
我已经解决了这个问题。你只需要参考其他人的自述文件。
首先,你应该将一个图片文件上传到 Github 代码库!然后直接引用图片文件的地址。



enter image description here

enter image description here


8
在我的情况下,我使用imgur并以以下方式使用直接链接。
![img](http://i.imgur.com/yourfilename.png)

7

您有两种简单的方法可以实现这个操作:

1)使用HTML img 标签,

2)![](您保存图片的路径/图片名称.png)

您可以从打开该图片的浏览器中复制URL以获取路径。如果路径或图片名称中有任何空格问题,请添加 -> %20,就像浏览器一样。

这两种方法都可以使用,如果您想了解更多信息,可以查看我的github -> https://github.com/adityarawat29


这对我很有效。在 README.md 文件下的“apps”文件夹中有一张图片,我使用了以下代码:![](images/ss_apps.png) - cdsaenz
1
我很惊讶只有Aditya提到了空格的问题,而且方式相当愚蠢——没有错误,但你的语法就像根本不存在这样的功能一样显示。因此,我给你点赞。 - Tammi

7

使用以下方法从仓库中显示图像:

在域名前添加:https://raw.githubusercontent.com/

在末尾添加标志:?sanitize=true&raw=true

请使用<img />标签

  • 域名: raw.githubusercontent.com/
  • 用户名: YourUserAccount/
  • 仓库名: YourProject/
  • 分支名: YourBranch/
  • 路径: DirectoryPath/
  • 文件名: example.png

适用于SVG、PNG和JPEG格式的图片。

该答案还可以在以下链接中找到: https://github.com/YourUserAccount/YourProject/blob/master/DirectoryPath/ReadMe.md

 - `raw.githubusercontent.com/YourUserAccount/YourProject/YourBranch/DirectoryPath/svgdemo1.svg?sanitize=true&raw=true`

使用后下面展示工作示例代码:

**raw.githubusercontent.com**:
<img src="https://raw.githubusercontent.com/YourUserAccount/YourProject/master/DirectoryPath/Example.png?raw=true" />

<img src="https://raw.githubusercontent.com/YourUserAccount/YourProject/master/DirectoryPath/svgdemo1.svg?sanitize=true&raw=true" />

raw.githubusercontent.com:

感谢: - https://dev59.com/UGYq5IYBdhLWcg3wbgDk#48723190 - https://github.com/potherca-blog/StackOverflow/edit/master/question.13808020.include-an-svg-hosted-on-github-in-markdown/readme.md


直到我删除了查询字符串,它才对我起作用。 - mhenry1384
1
至少对于.png和raw.githubusercontent.com,sanitize=true无效,应该被移除。raw=true可以正常工作。 https://github.com/sphinxy/AppDynamics.Playground/blob/master/README.md - Danila Polevshchikov
1
https://rawgit.com/已于2018年关闭,不应再使用;您应该从答案中删除它。https://raw.githubusercontent.com/是最接近的替代品。 - Rory O'Kane

6

如果您需要上传一些图片用于文档,一个好的方法是使用git-lfs。假设您已经安装了git-lfs,请按照以下步骤操作:

  1. Intialize git lfs for your each image type:

    git lfs *.png
    git lfs *.svg
    git lfs *.gif
    git lfs *.jpg
    git lfs *.jpeg
    
  2. Create a folder that will be used as image location eg. doc. On GNU/Linux and Unix based systems this can be done via:

    cd project_folder
    mkdir doc
    git add doc
    
  3. Copy paste any images into doc folder. Afterwards add them via git add command.

  4. Commit and push.

  5. The images are publicly available in the following url:

    https://media.githubusercontent.com/media/^github_username^/^repo^/^branch^/^image_location in the repo^

在以下内容中: * ^github_username^是Github用户名(您可以在个人资料页面找到) * ^repo_name^是存储库名称 * ^branch^是存储图像的存储库分支 * ^image_location in the repo^是包括存储图像的文件夹的位置。

此外,您可以先上传图像,然后访问您项目的Github页面上的位置,并浏览直到找到图像,然后按下下载按钮,然后从浏览器地址栏复制粘贴URL。

请参考我的项目中的此处

然后,您可以使用上述Markdown语法包含它们的URL:

![some alternate text that describes the image](^github generated url from git lfs^)

例如:假设我们使用这张照片,然后你可以使用Markdown语法:

![In what order to compile the files](https://media.githubusercontent.com/media/pc-magas/myFirstEnclave/master/doc/SGX%20Compile%20workflow.png)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接