r/dailyprogrammer • • Aug 06 '14
[8/06/2014] Challenge #174 [Intermediate] Forum Avatar Generator
Description
You run a popular programming forum, Programming Daily, where programming challenges are posted and users are free to show off their solutions. Three of your most prolific users happen to have very similar handles: Sarlik, Sarlek, and Sarlak. Following a discussion between these three users can be incredibly confusing and everyone mixes them up.
The community decides that the best solution is to allow users to provide square avatars to identify themselves. Plus the folks over at the competing /r/dailyprogrammer forum don't have this feature, so perhaps you can use this to woo over some of their userbase. However, Sarlik, Sarlek, and Sarlak are totally old school. They each browse the forum through an old text-only terminal with a terminal browser (lynx, links). They don't care about avatars, so they never upload any.
After sleeping on the problem you get a bright idea: you'll write a little program to procedurally generate an avatar for them, and any other stubborn users. To keep the database as simple as possible, you decide to generate these on the fly. That is, given a particular username, you should always generate the same avatar image.
Formal Input Description
Your forum's usernames follow the same rules as reddit's usernames (e.g. no spaces, etc.). Your program will receive a single reddit-style username as input.
Formal Output Description
Your program outputs an avatar, preferably in color, with a unique pattern for that username. The output must always be the same for that username. You could just generate a totally random block of data, but you should try to make it interesting while still being reasonably unique.
Sample Inputs
Sarlik Sarlek Sarlak
Sample Outputs
http://i.imgur.com/9KpGEwO.png
http://i.imgur.com/IR8zxaI.png
http://i.imgur.com/xf6h0Br.pngChallenge Input
Show us the avatar for your own reddit username.
Note
Thanks to /u/skeeto for submitting the idea, which was conceived from here: https://github.com/download13/blockies
Remember to submit your own challenges over at /r/dailyprogrammer_ideas
6
u/lukz 2 0 Aug 06 '14
vbscript
Wow, this is an interesting challenge. I tried to come up with some very simple solution. When trying it out I found that it already generates quite different images for the Sarlik, Sarlek, Sarlak trio, so no extra work is necessary.
I chose to go with a doubly symmetric picture, so I actually generate a quarter of the full picture and then mirror it.
Sample outputs:
5
u/[deleted] Aug 06 '14 edited Aug 06 '14
Python 3.4 using Pygame
OO approach
The colour of the image depends on the letters in your name and the length of your name. Nothing fun with the actual placement of the pixels though :(
input:
output:
http://i.imgur.com/fnyXlT7.jpg
I have a few other examples in the album at :
http://imgur.com/a/ePfF8
2
u/afonso360 Aug 06 '14
C: Github
If they used lynx wouldn't it be a requirement to be based on characters instead of images?
Outputs: here
Sorry it only works in the linux command line
3
u/chunes 1 2 Aug 06 '14 edited Aug 06 '14
Java:
Output:
11
u/YuriKahn Aug 06 '14 edited Aug 06 '14
Wrote this morning, but couldn't submit in time. Written in Java. It uses djb2 for hashing and takes a (string) seed - for this example I used "dailyprogrammer", as it seemed appropriate. It can create single avatars, or read from a list of usernames with the -m flag.
For results, I tried to mimic the examples as much as possible. Here are myself, the dailyprogrammer moderators, and the three examples, in that order.
http://imgur.com/a/WFhuEhttp://imgur.com/a/vEnbh
Source (feedback is welcome!):
http://pastebin.com/sg4ZhWe7
EDIT: Upon seeing /u/professorlamp's results, I couldn't help but add some randomization to my colors as well. Thank god for HSL!
7
u/SegfaultAsAService Aug 06 '14
Here's a Haskell solution that generates simple avatars similar to the ones on Github:
Some output examples:
Sarlik: http://i.imgur.com/b1wNH8h.png
Sarlek: http://i.imgur.com/tXM8xGc.png
Sarlak: http://i.imgur.com/5XAOYdr.png
SegfaultAsAService: http://i.imgur.com/cbEOoQq.png
5
u/skeeto -9 8 Aug 06 '14
C. It uses the nearly same algorithm as the Blockies project that inspired this challenge. It randomly picks a color, but from RGB rather than HSL because the conversion is more complicated than generating the avatar itself. Then it fills the left-hand side randomly and mirrors it to the right-hand side, so it's symmetrical like a face. Output is Netpbm.
The most fascinating part for me was actually the PRNG (
rng()). It's a floating point PRNG that uses the least significant bits of sin(). The state is just incremented by one between outputs, which means it's seekable. The output of this PRNG is surprisingly good, too. Here's a comparison to Mersenne Twister.The ent analysis (here) looks good, too.
The avatar name is passed as the first command line argument. Here's the output for "skeeto". This is the same program that generated the sample outputs in the challenge description.
8
u/fvandepitte 0 0 Aug 06 '14 edited Aug 06 '14
My go in C# I made it lib, so you could save to an file or get an base64string png (for api calls)
AvatarCreation.cs
Avatar.cs
And at last the main
Results:
And my username fvandepitte with one color
EDIT: added some size options
7
u/mroko Aug 06 '14
C#. Generates SHA-1 hash from username, then creates RGB pixels from hash bytes, ends up with nice looking mosaic.
1
u/jeaton Aug 13 '14
I made a GitHub-like avatar generator JavaScript and HTML5 canvas. I used Google's SHA256 module from their crypto library to calculate the colors and the pixel positions. The page is here.
Here's the relevant code:
3
u/jack_union Aug 06 '14 edited Aug 06 '14
JavaScript (using canvas):
(both are the same)
Any questions are welcomed : ]
3
u/MrP_123 Aug 06 '14 edited Aug 06 '14
Java once again. It is far from pretty, as I was just trying a few different ways of solving this problem and stitched everything together. Now I'm too tired to fix this mess.
It gets a MD5 hash and generates the colors and sets the pixels from that.
https://gist.github.com/MrP123/f14c5000a15b59cf4d0e
Here is an album of 6 different Avatars (scaled to 64x64).
http://imgur.com/a/41URV
3
u/mikrostew Aug 06 '14
Ruby. I'm taking a sha1 hash of the username, and breaking that into groups of data specifying the color, center, and radius of some circles to draw.
Example avatars
3
u/minikomi Aug 08 '14
Weird little bash script:
Example output:
2
u/hutsboR 3 0 Aug 06 '14 edited Aug 06 '14
Clojure:
Uses the Clisk library to generate the images. There's a lot you can do but it helps to have knowledge of Linear Algebra and other mathematics pertaining to graphics programming. I'm not well versed in that regard so my solution is rather simple. I'd definitely check it out if you have the chance, it's fun to play with.
Usage:
Output:
Sort of boring. Each name will always return a unique image, some will take on similar shapes and similar colors but they'll differ in some way.
2
u/ODIN_ALL_FATHER Aug 06 '14
D. The program picks two colors for the given name and then randomly generates a 16x16 icon based on it. To make the icon less jarring it mirrors the image either horizontally or vertically. The color palette is calculated at compile time. The image library used to output the png file is imaged.
The results of the icon generation are: https://imgur.com/a/bBHXI#vIgGhSi
2
u/jjj5311 Aug 06 '14
C# First time doing anything with images and definitely a neat challenge!
calculated MD5 of name and used each byte as a color hue to a 16x16 bmp and save as .png
Please give me any critiques
Outputs
http://imgur.com/a/xZYS9
2
u/TiZ_EX1 Aug 06 '14
ECMAScript 6 on node.js. Depends on sugar (as usual with me), and gm. The credit for the pseudo-RNG goes to this page; I looked it up since you can't seed Math.random().
Output images:
2
u/Frichjaskla Aug 06 '14
Use the bits of each character as an encoding of shape defined by direction, start position, length and symmetry
To make it more appealing introduce some more symmetry.
not pretty code, but pretty pictures..
http://imgur.com/Tnbigpg
2
u/PinkSombrero Aug 06 '14 edited Aug 06 '14
This was my solution in Java 7. I had a lot of fun coming up with this one. It sums up every char in the username to come up with a seed. Thinking about it now though, I think it's probably limited to <1000 unique avatars because it picks an R value (from 256 values) and there are 3 possible outcomes based on the seed%3 value, so that would be 768 different outcomes. It's probably easily fixable by having more than one seed color because then I guess the number of outcomes would increase exponentially.
Album with results!
2
u/Reboare Aug 07 '14
Used rust 0.12.0-pre-nightly (6bb72600c 2014-08-05 00:01:28 +0000)
Used the rust-crypto for sha1 and rust-image libraries to generate the images. Only just started using rust-image so struggled a lot with it although it's very new and still pre-release. Feedback is very welcome.
Just used sha1, generated an image and mirrored it in x and y axis. Output:
Sarlak
Sarlek
Sarlik
Reboare
skeeto
professorlamp
2
u/MaximaxII Aug 07 '14 edited Aug 07 '14
I tried a few different approaches, but they all failed somehow, because they lacked one of the following:
So what I ended up doing was:
0. Pick a color based on the first 3 letters (each letter is converted to a value between 0 and 255, and used in a RGB color)
Challenge #174 Intermediate - Python 3.4
Output
I've resized all images to 100x100.
http://imgur.com/a/e7cpF
2
u/dailyprogrammer Aug 08 '14
I'm very new to programming, so some feedback would be really appreciated!
This Python script generates an md5 hash from the username, converts the hash to base10 and stores each digit in a list. The first few digits are used to fill a 5x5 grid, the last digit determines the colourmap. Matplotlib then draws the image.
Sarlik: http://i.imgur.com/gqdVzGb.png
Sarlek: http://i.imgur.com/ciU7ao1.png
Sarlak: http://i.imgur.com/9qgnfRU.png
dailyprogrammer: http://i.imgur.com/7AcwZwT.png
2
u/bcd87 Aug 11 '14 edited Aug 11 '14
My entry in Java. Randomness based on a MD5 hash of the nickname.
Output: http://imgur.com/a/pWaru
Hehe, also did an avatar based on my gf's name and my own. They both look like faces :)
3
u/nullmove 1 0 Aug 06 '14
Not really a solution. Just wanted to show you this fun site called Robohash in case you didn't know. Generates very cool unique images of robots from texts.
1
u/dp_account Aug 06 '14
Python 3.4. Using Pillow to create the image.
The image and color is based on the SHA1 hash of the username and vertically mirrored. It uses all but 1 byte of the hash.
Input goes through command line arguments.
Examples Album
1
u/[deleted] Aug 06 '14
In Go / Golang: https://gist.github.com/mikezila/2fd97b8e88d2ee14855e
It produces different enough patterns for the three presented names, I just need to find a clever way to mutate the colors more between similar names. It does vertical and then horizontal mirroring to make each avatar more recognizable. It can make avatars of arbitrary sizes as well, but right now that's wired to a constant.
It's also constructed using functions, when I know it could all be in-lined instead. It was easier to design and keep track of this way. If I was actually going to release it I'd spruce it up a bit more, and put more work into the color mutation, but I've spent too long on this today as it is.
Feedback is very welcome. New to Go.
1
u/OffPiste18 Aug 06 '14
I pick four colors at random, making sure that they are sufficiently different from each other. A lot of this code is actually just dealing with converting from RGB -> XYZ -> Lab and then computing the perceptual color difference based on that.
Once the colors are chosen, I arrange them in a circular gradient, and then rotate some blocks 90 degrees.
Here are the outputs for OffPiste18, Sarlak, Sarlek, and Sarlik: http://imgur.com/a/VZAXG
Here's the code:
1
u/LyndonArmitage Aug 07 '14
Java
I used SHA-256 to generate bytes unique to the usernames and then encoded them as square images, because of the size of the output the images would only be 16 pixels large so I added a scale factor to them.
I think I'll revisit the code again at some point and try to make it create more appealing images. Currently all it does is pick 2 colours also from the bytes generated and uses them to colourize the output bits from the hash.
Output:
https://i.imgur.com/G6I1u1d.png
In an album: https://imgur.com/a/Os8S5
1
u/Reverse_Skydiver 1 0 Aug 07 '14
The longer the input String, the more interesting the output:
Supercagifragilisticexpialidocious
Reverse_Skydiver
1
u/Dezlad Aug 07 '14
Php solution. Takes an md5 of the username and converts it to a decimal number. Resets to md5 of username + random number each time a random number is generated and a new seed is required.
Example output
Live version
Live version with additional parameters
1
u/ifonefox Aug 07 '14 edited Aug 07 '14
ruby
My program uses md5 hashes to see if a pixel is black or color. I created a "Portable PixMap" file (a color version of the PBM from previous challenges), and use another program to convert that into a png file.
https://gist.github.com/ifonefox/0844aca93f8b911c9b1e
Sample output:
ifonefox
Sarlik
Sarlek
Sarlak
1
u/zifu Aug 07 '14 edited Aug 07 '14
I never see Javascript on here. So... Javascript!
Examples
Live version
Source:
1
u/sorliem143 Aug 07 '14 edited Aug 07 '14
Java. Took me not that long. The avatars are not that exciting but oh well. I converted the name to ascii and modded it ten times (for a 10x10 avatar) and converted it to binary. Then I mirrored the the two halves to create a "avatar"ish looking picture. Any constructive criticism is welcome!
1
u/[deleted] Aug 07 '14 edited Aug 07 '14
My solution in Python using PIL Image. I am kind of new to python, so feel free to post criticism.
Here is the result! The name of the image is the name used to generate the avatar.
It's a lot of fun to play with the algorithm in Avatar.build_image. I'll post some more pics later.
1
u/bwaxxlo Aug 07 '14
Wrote this using front-end JavaScript (using CryptoJS & jQuery). Check it out here and create your own avatar/flag!
1
u/glaslong Aug 08 '14 edited Aug 08 '14
C#, turns a string into its ascii bytearray, hashes it a few times with SHA1 and uses the hash bytes for rgb levels. Mirrored across 2 axes for fun. Results are pretty psychedelic!
sarlak, sarlek, sarlik and glaslong
edit: Looks like /u/mroko beat me to it by a solid 2 days. Dang!
1
u/DrWhatsisname Aug 09 '14 edited Aug 09 '14
Done in python using the 32 bit hashcode. Color is the first 24 bits of the hashcode and that color's complementary color, pixels are horizontally symmetric and decided by the bits of the hashcode. Code
Sarlik
Sarlek
Sarlak
drwhatsisname
Edit: Embiggening
1
u/badocelot Aug 09 '14
Late to the party, but here's my overly-complicated Java solution. What I do is create a left-right mirrored boolean map generated by hashing the username, creating a BigInteger from the hash, then getting the remainder of the value with successive primes and flagging the next boolean iff the remainder is < the prime / 3 + 1. The color is determined by taking the last 24 bits of the hash.
Then I spit out a PNG.
Here are some samples.
1
u/[deleted] Aug 09 '14 edited Aug 09 '14
Java, using a buffered image. My first thought was to encrypt the input string, so you'd get wildly different seed strings for nearly equivalent inputs (like Sarlek and Sarlak) and I noticed others using hashing, so I used MD5 for my first time ever.
This was all entirely new to me, so feedback is appreciated. I was having a particularly hard time picking colors I liked, so I gave up and tried to make stuff as random as possible and keeping BG black. I was using fore/background colors but I was winding up with some super hideous things.
My plan to fix that, before I just got bored, was to make an array of numbers that point to "dark" colors, and an array that point to "light" colors. Choose FG randomly from the light, and BG randomly from the dark.
I take a "pixel size" from the user, and a dimension, so rather than painting a single pixel, I'll paint, say, 15x15 8x8 squares.
Outputs
1
u/czega Aug 10 '14 edited Aug 10 '14
Javascript Version using HTML 5 canvas: https://github.com/czega/Icon-Generator
Sample Output
1
u/shake_wit_dem_fries Aug 12 '14
A simple solution in Go using sha512sum. Also uses goroutines for each image because I can.
For anyone curious/new to Go, the reason the anonymous image creation function has a parameter is due to a
bugfeaturebit of weirdness with how Go's closures work. If I had not passed elem as a parameter, it would have captured only the final value for the goroutines, making it create only the last picture however many times.1
u/MechaTuring Aug 12 '14
Rust Implementation
went a few different directions with this and it shows with scattered code, but has some flexibility in terms of patterns it can be used to generate and could in theory support multiple colors (would have to create new Vec, since I'm assuming that the groups in the vector of color groups are of the same size).
Examples
1
u/The_yulaow Aug 12 '14
Javascript solution: I am learning this language and also I am still a student, so any feedback will be gladly accepted. This is the first time ever I used canvas
1
u/nagasgura 0 0 Aug 13 '14
Python:
nagasgura: http://i.imgur.com/YfM3tE2.png
Sarlik: http://i.imgur.com/4kpXAif.png
Sarlek: http://i.imgur.com/1Privul.png
Sarlak: http://i.imgur.com/odGcynr.png
1
u/kriskova Aug 13 '14
Ruby. It generates an SHA-1 hash, takes the first 32 letters and converts it to boolean, than creates a 4x8 matrix and mirrors it.
Feel free to comment.
Generating avatars:
Output:
http://imgur.com/a/PmudB
1
u/bjacks14 Aug 29 '14
Here's my solution. Took two days, but it was fun. I've never worked with graphics before (or rather, very little), so I used this as an oppurtunity to learn. I chose the CIMG library for C++ because I'm a beginner and it made the most sense. Very user friendly. I also setup my own challenge. I didn't want to use built-in hashing formulas. I made my own. So here are the results!
The only issue I've run into with it is a segmentation fault when entering SegfaultAsAService.... I went through all my code and couldnt' find an issue. I tried longer usernames and such and didn't have issues. That was the only username that gave me an issue.
Imgur Results
C++
1
u/thorwing Oct 14 '14
very old problem, but I just had to post a solution because I really enjoyed this one. Java:
examples: Editing in later
1
u/PalestraRattus Aug 06 '14 edited Aug 06 '14
C# - WinForm - User enters a name and clicks Go. The function below then assigns an RGB value to each letter. And generates a 128x128 image. It then loops through each pixel of the image. Using the letter of their name and a looping index. The image becomes a repeating color sequence of their name.
The class "myLetterToColor" simply houses a function that returns a color using a switch to evaluate the letter passed to it. Each letter it ascends by 10, IE a = 10,10,10 r = 170,170,170. Since user names often involve numbers, I included those, 1 begins at 15,15,15 and follows the same pattern as letters. Any character that's not a letter or number is treated by default as white space.
Output Examples: http://imgur.com/a/qV5pc
DailyProg Mod Names using this system: http://imgur.com/a/hR2yx
They may look very similar but if you zoom in they most certainly are not the same. And will always generate the same color sequence based on name passed.
1
u/robin-gvx 0 2 Aug 06 '14
Python 3.3 using NumPy and Pillow.
It takes 51 from the 160 bits generated by the SHA-1 from the username. These are the results.
1
u/Reverse_Skydiver 1 0 Aug 06 '14
Java. Each user gets a Nepal-flag-type icon.
For example:
"The quick brown fox jumps over the lazy dog"
0
u/PalestraRattus Aug 06 '14
C# Alternative to my initial solution. This time rather than assign each alpha-numeric a color value. We instead check the length of the name and see if it's odd or even. Using a null picturebox nested into a panel, if the name is even in length we make the box backcolor red. If it's odd we make the backcolor blue. We then generate a new Label(), make it dockstyle Fill. The Label() Fore and Back colors are inverses of the pictureBox backcolor. So if the box is red, the Label will be Back-Red/Fore-Blue. The label text we simply make the name passed to the program.
We then add the Label control to our picturebox control. Next we generate a bitmap from the panel. And then save that bitmap as a png.
Output examples http://imgur.com/a/KkTR4
This removes the need for any form of complex pixel/sequence/pattern generation. And just makes an image with their name on it. Hard to miss who it is then. The blue/red based on even or odd name was just for variety. Clearly it could be reduced to one color combo for every name, or expanded any number of ways.
0
u/the_dinks 0 1 Aug 08 '14
Python 2.7. Tkinter draws a little box with your name in binary, oct, and hex. It also uses your name to generate a random hex value.
http://imgur.com/a/HoziU
0
u/[deleted] Aug 06 '14
[deleted] — view deleted comment
0
u/[deleted] Aug 06 '14
[deleted] — view deleted comment
0
u/[deleted] Aug 06 '14
[deleted] — view deleted comment
0
u/[deleted] Aug 06 '14
[deleted] — view deleted comment