Skip to content

Instantly share code, notes, and snippets.

@Blaizzy
Created April 9, 2019 14:48
Show Gist options
  • Select an option

  • Save Blaizzy/1a3e792bf6d02d80f6935b4b9eedd9b7 to your computer and use it in GitHub Desktop.

Select an option

Save Blaizzy/1a3e792bf6d02d80f6935b4b9eedd9b7 to your computer and use it in GitHub Desktop.
class Rescale(object):
"""Rescale the image in a sample to a given size.
Args:
output_size (tuple or int): Desired output size. If tuple, output is
matched to output_size. If int, smaller of image edges is matched
to output_size keeping aspect ratio the same.
"""
def __init__(self, output_sz):
assert isinstance(output_sz,(int,tuple))
self.output_sz = output_sz
def __call__(self, image):
h,w = image.shape[:2]
if isinstance(self.output_sz, int):
if h > w:
new_h, new_w = self.output_sz * h / w, self.output_sz
else:
new_h, new_w = self.output_sz, self.output_sz * w / h
else:
new_h, new_w = self.output_sz
new_h, new_w = int(new_h), int(new_w)
img = transform.resize(image, (new_h, new_w))
return img
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment