Created
September 23, 2019 09:49
-
-
Save booiljung/b195939e6e1f0ccb1cba0fb8ea1ca3a8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def left_right_crop(img, size): | |
if isinstance(size, numbers.Number): | |
size = (int(size), int(size)) | |
else: | |
assert len(size) == 2, "Please provide only two dimensions (h, w) for size." | |
w, h = img.size | |
crop_h, crop_w = size | |
if crop_w > w or crop_h > h: | |
raise ValueError("Requested crop size {} is bigger than input size {}".format(size, (h, w))) | |
l = img.crop((0, 0, crop_w, crop_h)) | |
r = img.crop((w - crop_w, 0, w, crop_h)) | |
return (l, r) | |
class LeftRightCrop(object): | |
def __init__(self, size): | |
self.size = size | |
if isinstance(size, numbers.Number): | |
self.size = (int(size), int(size)) | |
else: | |
assert len(size) == 2, "Please provide only two dimensions (h, w) for size." | |
self.size = size | |
def __call__(self, img): | |
return left_right_crop(img, self.size) | |
def __repr__(self): | |
return self.__class__.__name__ + '(size={0})'.format(self.size) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment