当GitHub用户注册一个帐户时,GitHub会提供一个默认的个人资料图片,如下所示:
用户可以根据instructions here设置自定义头像。
我能想到的方法是从GitHub用户那里下载当前的头像,然后从https://github.com/identicons/USERNAME.png下载他的默认头像。然后比较这两张图片。但是这个解决方案并不美观。
有没有一种很好的方法来确定GitHub用户是使用默认头像还是设置了自定义头像?比如布尔值,我可以检查,或者类似这样的东西。谢谢。
发布于 2018-03-05 13:47:13
GitHub将使用Gravatar service来获取与GitHub用户电子邮件帐户关联的图像。
您可以通过Gravatar API使用query such an image。如果没有,这意味着将使用默认的自动生成的图像。
当没有与电子邮件关联的图片时,GitHub使用?d=identicon
parameter根据电子邮件散列生成几何图案:
https://www.gravatar.com/avatar/05b6d7cc7c662bf81e01b39254f88a49?d=identicon
发布于 2021-06-08 08:33:13
我需要这样做,我认为作为一个生成的图像,像素将被清晰地着色,没有抖动。
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图像。然后我们以像素为单位制作一组颜色,所以我们只得到唯一的颜色。如果只有两种独特的颜色,那么它就是一幅雕像,更多的是一张照片。(我将其设置为10是为了给我一些喘息的空间,因为我不想要假阴性。)
你可以使用construct the URL for the image using this technique。
https://github.com/twbs.png
或
https://github.com/npm.png?size=200
我是如何使用它的
在第一年的课程中,我想检查用户是否更新了他们的照片,以便教程团队可以轻松地将repos与人匹配。
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
https://stackoverflow.com/questions/49103466
复制相似问题