如何通过编程确定 GitHub 帐户是否使用默认个人资料图片(头像)?

7
当GitHub用户注册账户时,GitHub提供一个默认的个人资料图片,看起来像这样:enter image description here。用户可以根据此处的说明设置自定义个人资料图片。
我能想到的方法是从GitHub用户下载当前的个人资料图片,然后从https://github.com/identicons/USERNAME.png下载他的默认个人资料图片。接着对比这两张图片。但这个解决方案不够优美。
是否有一种更好的方式来确定GitHub用户是否使用默认个人资料图片或已设置自定义个人资料图片?是否有一个布尔值或类似的东西可以检查?谢谢。
3个回答

0

谢谢回复。但我还是没有很清楚地理解。 - hsluoyz
谢谢回复。但我还是没有很清楚地理解。以这个用户为例:https://github.com/abc,如何知道他是否使用了自定义头像?注意:如果该用户隐藏了电子邮件,我不知道他的电子邮件。我只知道他的用户名。 - hsluoyz
"alastair.campbell@runbox.no" 的哈希值为 05b6d7cc7c662bf81e01b39254f88a49。然后我使用此链接获取个人资料图片:https://www.gravatar.com/avatar/05b6d7cc7c662bf81e01b39254f88a49。但似乎不正确... - hsluoyz
@hsluoyz 注意:https://github.com/abc 可能是另一个阿莱斯特·坎贝尔(Alaister Campbell),他的电子邮件是chartax@gmail.com,这个链接有效:https://www.gravatar.com/avatar/061052358b0aa897f04d438c38dc475b。 - VonC
我不想使用电子邮件,你能否提供一个基于GitHub登录的解决方案? - hsluoyz
显示剩余5条评论

0

-1
我需要这样做,并且我认为作为生成的图像,像素将被干净地着色而没有抖动。
import requests
from PIL import Image

# image_url = "https://avatars.githubusercontent.com/u/85327959" # a photograph
image_url = "https://avatars.githubusercontent.com/u/85325807?v=4" # a gravatar
img_data = requests.get(image_url).content
with open("avatar.jpg", "wb") as handler:
    handler.write(img_data)

image = Image.open("avatar.jpg")
colour_count = len(set(image.getdata()))

print(image.size, "colours:", colour_count)

if colour_count < 10:
    print("this is a gravatar")

这里发生了什么?

我们下载图像,然后将其作为PIL图像加载。然后我们制作像素颜色集,以便只获取唯一的颜色。如果只有2种唯一的颜色,则它是一个gravatar,如果有更多,则是一张照片。(我将其设置为10,以给自己留出一些余地,因为我不想要假阴性。)

您可以使用此技术构建图像的URL

https://github.com/twbs.png

或者

https://github.com/npm.png?size=200

我如何在现实生活中使用它

在一门大一课程中,我想检查用户是否已更新其照片,以便教学团队可以轻松地将代码库与人员匹配。

def has_real_photo(repo_path):
    repo = git.cmd.Git(repo_path)
    origin_url = get_origin_url(repo)
    owner = origin_url.split("/")[3]
    image_url = f"https://github.com/{owner}.png?size=40"
    img_data = requests.get(image_url).content
    with open("avatar.jpg", "wb") as handler:
        handler.write(img_data)

    image = Image.open("avatar.jpg")
    colour_count = len(set(image.getdata()))

    if colour_count > 10:
        block_image = blocky_photo(image)
        print(block_image)
        return True
    else:
        block_image = blocky_photo(image)
        print(
            f"Your GitHub profile picture only has {colour_count} colours.\n"
            "This makes me think it's the default avatar.\n"
            "Not like this:\n",
            block_image,
            """Like this:
            ╭───────────╮
            │  !!!!!!!  │
            │ /       \ │
            │ │  O  O │ │
            │<│    v  │>│
            │  \  ─── / │
            │   \____/  │
            ╰───────────╯\n"""
            "Go to https://github.com/settings/profile and upload a photo of your face.\n"
            "This really helps us understand who's who and be more useful in tutorials.",
        )
        return False


def blocky_photo(image):
    colour_map_list = list(
        zip(
            list(set(image.getdata())),
            ["█", "░", "▒", "▓", "X", "#", "%", "/", ":", "*"],
        )
    )
    colour_map = {x[0]: x[1] for x in colour_map_list}
    image = image.resize((20, 10), Image.NEAREST)
    pixels = list(image.getdata())
    width, height = image.size
    block_image = ""
    for i in range(len(pixels)):
        block_image += colour_map[pixels[i]]
        if (i + 1) % (width) == 0:
            block_image += "\n "
    return block_image

他们有一组测试,可以自行运行,并打印:

Your GitHub profile picture only has 2 colours.
This makes me think it's the default avatar.
Not like this:
 ████████████████████
 █████░░░░░░░░░░█████
 █████░░░░░░░░░░█████
 █████░░░████░░░█████
 █████░░░████░░░█████
 █████░░░████░░░█████
 ██░░░░░░░░░░░░░░░░██
 ██░░░░░░░░░░░░░░░░██
 ██░░░░░░████░░░░░░██
 ████████████████████
  Like this:
            ╭───────────╮
            │  !!!!!!!  │
            │ /       \ │
            │ │  O  O │ │
            │<│    v  │>│
            │  \  ─── / │
            │   \____/  │
            ╰───────────╯
Go to https://github.com/settings/profile and upload a photo of your face.
This really helps us understand who's who and be more useful in tutorials.
✘ You've got a photo for your GitHub account

P.S. 这里的表情来自于 https://dev.to/ollie/generate-ascii-faces-17j4 - Ben

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