Skip to content

Commit

Permalink
Add rounding tolerance to image_io.trim_image()
Browse files Browse the repository at this point in the history
Fixes issue with zoom() from #2.
  • Loading branch information
bwoodsend committed Jan 13, 2021
1 parent c1f305d commit 17ea1da
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions vtkplotlib/_image_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,9 +329,14 @@ def trim_image(arr, background_color, crop_padding):

background_color = np.asarray(background_color)
if background_color.dtype.kind == "f" and arr.dtype.kind == "u":
background_color = (background_color * 255).astype(arr.dtype)
background_color = background_color * np.iinfo(arr.dtype).max

mask = (arr == background_color).all(-1)
if arr.dtype.kind == "u":
# Allow up to 1 LSB of deviation to account for rounding/integer
# truncation error.
mask = (np.abs(arr - background_color) <= 1).all(-1)
else:
mask = (arr == background_color).all(-1)

if isinstance(crop_padding, float):
crop_padding = int(crop_padding * min(mask.shape))
Expand Down

0 comments on commit 17ea1da

Please sign in to comment.