小编Ksh*_*a_7的帖子

如何使用 PyGithub 将图像文件上传到 Github?

我想使用 Pygithub 将图像文件上传到我的 Github 存储库。

from github import Github

g=Github("My Git Token")
repo=g.get_repo("My Repo")
content=repo.get_contents("")
f=open("1.png")
img=f.read()
repo.create_file("1.png","commit",img)
Run Code Online (Sandbox Code Playgroud)

但我收到以下错误:

File "c:\Users\mjjha\Documents\Checkrow\tempCodeRunnerFile.py", line 10, in <module>
    img=f.read()
  File "C:\Program Files\Python310\lib\encodings\cp1252.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 119: character maps to <undefined>
Run Code Online (Sandbox Code Playgroud)

此方法对于文本文件效果很好。但我无法将图像文件上传到我的存储库。

当我使用 open-CV 读取图像文件时,出现以下错误:

    assert isinstance(content, (str, bytes))
    AssertionError
Run Code Online (Sandbox Code Playgroud)

我使用 cv2 时的代码是:

from github import Github
import cv2
g=Github("")
repo=g.get_repo("")
content=repo.get_contents("")


f=cv2.imread("1.png")
img=f
repo.create_file("1.png","commit",img)
Run Code Online (Sandbox Code Playgroud)

我认为 createFile() 仅将字符串作为参数,因此会出现这些错误。

有没有办法使用 Pygithub(或任何库)将图像文件上传到 Github?

python github github-api github-pages pygithub

4
推荐指数
1
解决办法
1461
查看次数

标签 统计

github ×1

github-api ×1

github-pages ×1

pygithub ×1

python ×1