If the page returns with a server error (5xx) you should check the web server log to see what happened.
In this case, my best guess is that the server has not permissions to open graph.png for output; in general it is a bad idea using a relative path in a cgi script, for you really don't know which the current dir is at execution time. Try again, specifying an absolute patch name in your open, and a path in which the user running the web server has permission to write into.
Rule One: "Do not act incautiously when confronting a little bald wrinkly smiling man."