Creating an initials avatar with Elixir
2 answers
You can use ImageMagick for this. Just call the command convert
via System.cmd
and pass parameters to it. Here's a simple example of creating an image similar to the one you posted. I'll leave the fine tuning to you.
def generate(outfile, initials) do
size = 512
resolution = 72
sampling_factor = 3
System.cmd "convert", [
"-density", "#{resolution * sampling_factor}", # sample up
"-size", "#{size*sampling_factor}x#{size*sampling_factor}", # corrected size
"canvas:#E0E0E0", # background color
"-fill", "#6D6D6D", # text color
"-font", "/Library/Fonts/Roboto-Bold.ttf", # font location
"-pointsize", "300", # font size
"-gravity", "center", # center text
"-annotate", "+0+#{25 * sampling_factor}", initials, # render text, move down a bit
"-resample", "#{resolution}", # sample down to reduce aliasing
outfile
]
end
For example this
generate('out.png', 'JD')
will create the following image:
+4
source to share