Error reports that there is no png function in GD::Image... I looked it up and there really isn't any... But http://search.cpan.org/~lds/GD-2.44/GD.pm says that there should be... Actually they have example same as your code (which I just copied)
P.S. i forgot to ask... what does rgb2n function do? and will it work without it?
P.P.S. still not working, but i did it manualy with printing in .ppm file