How to get the spectrogram from an audio file?

So i’m trying to map some audio into an image (spectogram) in this way :
*Get the spectrum data (512 samples).
Make an png 512512 => the value of the pixel on the row i and col j = the amplitude on the frequency j for the sample j ( it take aprox 10 seconds for the image to be created)

[512 samples for the spectrum data => 43 Hz / sample]

but the results are not so good (i uploaded one of those bellow).

My goal is to feed an Convolutional Neural Network with those images but with my technique i gen an precision of 56% which is not good at all:(

Some advices please?

Heya!
Could you post your code?