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

Skip to content

Commit 9a7991d

Browse files
committed
Fix for issue #2963
Image::resize will now raise a runtime error if the user passes in an invalid width or height. BboxImage.make_image will now ensure that the width and height it passes to Image::resize are positive or zero. BboxImage.draw now calculates the bottom left corner of its Bbox, rather than using the "first" corner of its Bbox.
1 parent a809ef9 commit 9a7991d

2 files changed

Lines changed: 12 additions & 3 deletions

File tree

lib/matplotlib/image.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,8 +1154,8 @@ def make_image(self, renderer, magnification=1.0):
11541154
im.set_resample(self._resample)
11551155

11561156
l, b, r, t = self.get_window_extent(renderer).extents # bbox.extents
1157-
widthDisplay = round(r) - round(l)
1158-
heightDisplay = round(t) - round(b)
1157+
widthDisplay = abs(round(r) - round(l))
1158+
heightDisplay = abs(round(t) - round(b))
11591159
widthDisplay *= magnification
11601160
heightDisplay *= magnification
11611161

@@ -1183,11 +1183,14 @@ def draw(self, renderer, *args, **kwargs):
11831183
# todo: we should be able to do some cacheing here
11841184
image_mag = renderer.get_image_magnification()
11851185
im = self.make_image(renderer, image_mag)
1186-
l, b, r, t = self.get_window_extent(renderer).extents
1186+
x0, y0, x1, y1 = self.get_window_extent(renderer).extents
11871187
gc = renderer.new_gc()
11881188
self._set_gc_clip(gc)
11891189
gc.set_alpha(self.get_alpha())
11901190
#gc.set_clip_path(self.get_clip_path())
1191+
1192+
l = np.min([x0, x1])
1193+
b = np.min([y0, y1])
11911194
renderer.draw_image(gc, round(l), round(b), im)
11921195
gc.restore()
11931196

src/_image.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,12 @@ Image::resize(const Py::Tuple& args, const Py::Dict& kwargs)
374374
int numcols = Py::Int(args[0]);
375375
int numrows = Py::Int(args[1]);
376376

377+
if (numcols < 0 || numrows < 0)
378+
{
379+
throw Py::RuntimeError(
380+
"Width and height must have non-negative values");
381+
}
382+
377383
colsOut = numcols;
378384
rowsOut = numrows;
379385

0 commit comments

Comments
 (0)