MCPcopy Create free account
hub / github.com/apache/singa / crop_and_resize

Function crop_and_resize

python/singa/image_tool.py:86–121  ·  view source on GitHub ↗

Crop a max square patch of the input image at given position and resize it into given size. Args: patch(tuple): width, height position(list(str)): left, center, right, top, middle, bottom.

(img, patch, position)

Source from the content-addressed store, hash-verified

84
85
86def crop_and_resize(img, patch, position):
87 '''Crop a max square patch of the input image at given position and resize
88 it into given size.
89
90 Args:
91 patch(tuple): width, height
92 position(list(str)): left, center, right, top, middle, bottom.
93 '''
94 size = img.size
95 if position == 'left':
96 left, upper = 0, 0
97 right, bottom = size[1], size[1]
98 elif position == 'center':
99 left, upper = (size[0] - size[1]) // 2, 0
100 right, bottom = (size[0] + size[1]) // 2, size[1]
101 elif position == 'right':
102 left, upper = size[0] - size[1], 0
103 right, bottom = size[0], size[1]
104 elif position == 'top':
105 left, upper = 0, 0
106 right, bottom = size[0], size[0]
107 elif position == 'middle':
108 left, upper = 0, (size[1] - size[0]) // 2
109 right, bottom = size[0], (size[1] + size[0]) // 2
110 elif position == 'bottom':
111 left, upper = 0, size[1] - size[0]
112 right, bottom = size[0], size[1]
113 else:
114 raise Exception('position is wrong')
115 box = (left, upper, right, bottom)
116 new_img = img.crop(box)
117
118 new_img = new_img.resize(patch, Image.BILINEAR)
119 # print box+crop
120 # print "crop to box %d,%d,%d,%d and scale to %d,%d" % (box+crop)
121 return new_img
122
123
124def resize(img, small_size):

Callers 1

crop3Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected