This code mostly works, but the resulting data seems to loose a color channel (is what I am thinking) as the resulting image data when displayed is tinted blue!
Here
It sounds like it might be that relationship. Possibly have it be a jpg and RGB instead of indexed colors with a png?