github默认头像 avatar

 

  生成独一无二的头像https://github.com/identicons/chucklu.png 将最后的图片的名字修改之后,会自动生成新的

Identicons! | The GitHub Blog

GitHub is about to get a lot more colorful. Starting today, we are generating Identicons for anyone without a Gravatar:

Our Identicons are simple 5×5 “pixel” sprites that are generated using a hash of the user’s ID. The algorithm walks through the hash and turns pixels on or off depending on even or odd values. These generated patterns, combined with hash-determined color values, ensures a huge number of unique Identicons.

Using a Gravatar but want to see what your identicon would be? Use this URL and replace my GitHub login with yours:

https://identicons.github.com/jasonlong.png

 

How does github generate github.com/identicons

GitHub generates Identicons using a specific algorithm that takes the user's username or email address as input and generates a unique image as output.

The algorithm works by hashing the input using the MD5 cryptographic hash function to produce a 128-bit hash value. This hash value is then used to generate a 5x5 grid of colored squares, with each square being either white or one of a few specific colors. The position of each square in the grid is determined by the value of one of the bytes in the hash value, and the color of each square is determined by the value of the corresponding byte in the hash value.

The resulting image is a unique and colorful abstract pattern that serves as a visual representation of the user's identity. Because the images are generated based on a hash of the user's username or email address, they are consistent and reproducible, which means that the same user will always be associated with the same Identicon.

 

Implement the avatar generation for github.com/identicons in Python with PIL

Sure, here is a Python code snippet that generates a Github-like identicon using the Python Imaging Library (PIL):

from PIL import Image

# Define the input string and hash it using SHA1
input_string = "hello"
hash_value = hashlib.sha1(input_string.encode('utf-8')).hexdigest()

# Extract the first 15 characters of the hash
short_hash = hash_value[:15]

# Initialize the size of the image and the size of each square
image_size = 420
square_size = image_size // 7

# Initialize the image and fill it with a light gray color
image = Image.new('RGB', (image_size, image_size), (240, 240, 240))

# Define the color for the foreground squares based on the hash value
color = (
    int(short_hash[0:2], 16),
    int(short_hash[2:4], 16),
    int(short_hash[4:6], 16)
)

# Loop through the hash and draw the squares
for i, char in enumerate(short_hash):
    # Calculate the position of the square
    x = (i % 5) * square_size
    y = (i // 5) * square_size

    # If the character is odd, draw the square
    if int(char, 16) % 2 == 1:
        draw = ImageDraw.Draw(image)
        draw.rectangle((x, y, x + square_size, y + square_size), fill=color)

# Show the image
image.show()

 This code generates an identicon using the string "hello". It first hashes the string using SHA1 and extracts the first 15 characters of the hash. It then initializes an image with a light gray color and loops through the hash, drawing squares based on the value of each character. If the character is odd, a square is drawn with a color based on the hash value. Finally, the image is displayed using the show() method.

 

Mathematica实战范例:[15]Github 头像制作_电脑软件-百度经验 (baidu.com)

How is the default user avatar generated? - Meta Stack Exchange

 

 

 

posted @ 2022-05-27 17:15  ChuckLu  阅读(758)  评论(0编辑  收藏  举报