diff options
| author | jules@lens <julescarbon@gmail.com> | 2018-05-09 21:24:11 +0200 |
|---|---|---|
| committer | jules@lens <julescarbon@gmail.com> | 2018-05-09 21:24:11 +0200 |
| commit | 0e03791f3f7db3f157bed785ba06d21270b98059 (patch) | |
| tree | e60038d09cbaca58ac742534b352d64383a1ffc2 /util/util.py | |
| parent | 3ec7af8d74608a44240bac690a0300cd19c92298 (diff) | |
| parent | d6e04496f0a93f97efbf6d67d12a51aa2a298123 (diff) | |
merge
Diffstat (limited to 'util/util.py')
| -rw-r--r-- | util/util.py | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/util/util.py b/util/util.py index 7a452a6..1c03715 100644 --- a/util/util.py +++ b/util/util.py @@ -54,3 +54,23 @@ def mkdirs(paths): def mkdir(path): if not os.path.exists(path): os.makedirs(path) + +def crop_image(img, xy, scale_factor): + '''Crop the image around the tuple xy + + Inputs: + ------- + img: Image opened with PIL.Image + xy: tuple with relative (x,y) position of the center of the cropped image + x and y shall be between 0 and 1 + scale_factor: the ratio between the original image's size and the cropped image's size + ''' + center = (img.size[0] * xy[0], img.size[1] * xy[1]) + new_size = (img.size[0] / scale_factor, img.size[1] / scale_factor) + left = max (0, (int) (center[0] - new_size[0] / 2)) + right = min (img.size[0], (int) (center[0] + new_size[0] / 2)) + upper = max (0, (int) (center[1] - new_size[1] / 2)) + lower = min (img.size[1], (int) (center[1] + new_size[1] / 2)) + cropped_img = img.crop((left, upper, right, lower)) + return cropped_img + |
