We have a fairly complex image processing script written in Python which is using PIL and numpy. For one of the steps, we have a very sensitive multi channel gradients which is a lookup table. Once it has been created, it is saved down to multiple different smaller resolutions. When this happens however, the green channel, which has a gradient running left to right, suddenly appears to lose percision. It is supposed to lose 1 of 255 values every 50 pixels or so. Instead, it starts dropping by values of 2 at every 100 pixels. This causes huge issues and I can't figure out why PIL is doing it. However, I do see jumps of 1 in other portions of the map so I don't think its a simple as its missing one bit of precision. I also noticed on another channel, it seemed like the whole map was shifted by 1 value. The entire thing seems inaccurate once scaled, even when using the "Nearest" filter.
For the full size image, we create it from our numpy array with the following:
image = Image.fromarray(imageIn.astype(np.uint8))
We then scale it down:
new_image = image.resize(new_size, scaleFilter)
The scale is always half the largest and I have tried all available scale options.
We then save it to a PNG as follows:
new_image.save(file_name, 'PNG')
We save both the large one directly after step 1 with the same save command and it is fine. After the scale, we have the issue on the green channel. Any help would be great!
EDIT:
It now appears that it is likley an issue in SciPy. The following still causes the issue:
new_array = misc.imresize(imageIn, (x_size, y_size, 4), interp='nearest')
misc.imsave(file_name,new_array)
I do not understand how I am even getting the distortions with nearest. I am allocating this array as a float64, but it has to involve rounding issues within the code
EDIT #2:
I took this a step further and tried OSX built in program sips to download it and got the same distortion! I then tried it with Adobe After Effects and it worked fine. I then installed imagemagick which now works fine. I will still award the bounty to anyone who can explain why this is happening within all these methods.
EDIT #3
Per the request, here is a section of a sprite map scaled and unscaled. During creating these, I found the OSX's built in "Preview" application also causes scaling issues when scaling down so I actually had to use photoshop to get the original clip.
Original:
Scaled with distortions. Try looking at the green channel along the horizontal axis
Note that these clippings are not of the exact same pixels, but cut from the same area as you can see by the shape
EDIT #4
I have now tried doing this scaling via OpenGL within the application and I have found it happens there too! This has to do with some fundamental issue of doing bilinear interpolation with a fixed number of bits?

