Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f0fec8b

Browse files
committed
Fix resample code to round half down
The code previously performed the flipping in output space, but the flipping needs to be in input space because that is where the rounding to nearest input pixel takes place. Also, flipping in the vertical direction already happens if origin='lower', so flip vertically only if origin='upper'.
1 parent 814662a commit f0fec8b

1 file changed

Lines changed: 8 additions & 6 deletions

File tree

lib/matplotlib/image.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,15 @@ def _resample(
209209

210210
# When an output pixel falls exactly on the edge between two input pixels, the Agg
211211
# resampler will use the right input pixel as the nearest neighbor. We want the
212-
# left input pixel to be chosen instead, so we flip the supplied transform.
212+
# left input pixel to be chosen instead, so we flip the input data and the supplied
213+
# transform. If origin != 'upper', the transform will already include a flip in the
214+
# vertical direction.
213215
if interpolation == 'nearest':
214-
transform += Affine2D().translate(-out.shape[1], -out.shape[0]).scale(-1, -1)
216+
transform = Affine2D().translate(-data.shape[1], 0).scale(-1, 1) + transform
217+
data = np.flip(data, axis=1)
218+
if image_obj.origin == 'upper':
219+
transform = Affine2D().translate(0, -data.shape[0]).scale(1, -1) + transform
220+
data = np.flip(data, axis=0)
215221

216222
_image.resample(data, out, transform,
217223
_interpd_[interpolation],
@@ -220,10 +226,6 @@ def _resample(
220226
image_obj.get_filternorm(),
221227
image_obj.get_filterrad())
222228

223-
# Because we flipped the supplied transform, we then flip the output image back.
224-
if interpolation == 'nearest':
225-
out = np.flip(out, axis=(0, 1))
226-
227229
return out
228230

229231

0 commit comments

Comments
 (0)