Your file path
/home/$user/test.png
contains a environment variable. That won't be resolved and your program will look for that exact path, which probably does not exist.
It is typical for shells to do these kinds of resolutions, though.