added writeImage function