Skip to content

Commit eb42ff4

Browse files
committed
Add zoomout operation
1 parent a8a1754 commit eb42ff4

File tree

2 files changed

+95
-3
lines changed

2 files changed

+95
-3
lines changed

Augmentor/Operations.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1948,6 +1948,68 @@ def do(image):
19481948
return augmented_images
19491949

19501950

1951+
class ZoomOut(Operation):
1952+
"""
1953+
This class is used to shrink entire image then return a black-framed
1954+
image which has the same size as the original image.
1955+
zoomout effect
1956+
"""
1957+
1958+
def __init__(self, probability, min_factor, max_factor):
1959+
"""
1960+
The amount of zoom applied is randomised, from between
1961+
:attr:`min_factor` and :attr:`max_factor`. Set these both to the same
1962+
value to always zoom by a constant factor.
1963+
1964+
:param probability: Controls the probability that the operation is
1965+
performed when it is invoked in the pipeline.
1966+
:param min_factor: The minimum amount of zoom to apply. Set both the
1967+
:attr:`min_factor` and :attr:`min_factor` to the same values to zoom
1968+
by a constant factor.
1969+
:param max_factor: The maximum amount of zoom to apply. Set both the
1970+
:attr:`min_factor` and :attr:`min_factor` to the same values to zoom
1971+
by a constant factor.
1972+
:type probability: Float
1973+
:type min_factor: Float
1974+
:type max_factor: Float
1975+
"""
1976+
Operation.__init__(self, probability)
1977+
self.min_factor = min_factor
1978+
self.max_factor = max_factor
1979+
1980+
def perform_operation(self, images):
1981+
"""
1982+
Zooms/scales the passed image(s) and returns the new image.
1983+
1984+
:param images: The image(s) to be zoomed.
1985+
:type images: List containing PIL.Image object(s).
1986+
:return: The transformed image(s) as a list of object(s) of type
1987+
PIL.Image.
1988+
"""
1989+
factor = 1 / round(random.uniform(self.min_factor, self.max_factor), 2)
1990+
1991+
def do(image):
1992+
w, h = image.size
1993+
w_zoom = int(round(image.size[0] * factor))
1994+
h_zoom = int(round(image.size[1] * factor))
1995+
1996+
new_im = Image.new("RGB", (w, h))
1997+
image_zoomed = image.resize((w_zoom, h_zoom), resample=Image.BICUBIC)
1998+
1999+
# where to paste the shrinked image into black
2000+
paste_pose_x = int((w - w_zoom)/2)
2001+
paste_pose_y = int((h - h_zoom)/2)
2002+
new_im.paste(image_zoomed, (paste_pose_x, paste_pose_y))
2003+
2004+
return new_im
2005+
2006+
augmented_images = []
2007+
2008+
for image in images:
2009+
augmented_images.append(do(image))
2010+
2011+
return augmented_images
2012+
19512013
class HSVShifting(Operation):
19522014
"""
19532015
CURRENTLY NOT IMPLEMENTED.

Augmentor/Pipeline.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ def _execute(self, augmentor_image, save_to_disk=True, multi_threaded=True):
237237

238238
if save_to_disk:
239239
filename, ext = os.path.splitext(os.path.basename(augmentor_image.image_path))
240-
file_name = "{}_{}.{}".format(filename, str(uuid.uuid4()), self.save_format)
240+
file_name = "{}_{}".format(filename, str(uuid.uuid4()).replace("-", ""))
241241
try:
242242
for i in range(len(images)):
243243
if i == 0:
@@ -248,7 +248,7 @@ def _execute(self, augmentor_image, save_to_disk=True, multi_threaded=True):
248248
+ file_name \
249249
+ "." \
250250
+ (self.save_format if self.save_format else augmentor_image.file_format)
251-
251+
save_name = "{}.{}".format(file_name, self.save_format)
252252
images[i].save(os.path.join(augmentor_image.output_directory, save_name))
253253

254254
else:
@@ -262,7 +262,7 @@ def _execute(self, augmentor_image, save_to_disk=True, multi_threaded=True):
262262
+ file_name \
263263
+ "." \
264264
+ (self.save_format if self.save_format else augmentor_image.file_format)
265-
265+
save_name = "{}_gt.{}".format(file_name, self.save_format)
266266
images[i].save(os.path.join(augmentor_image.output_directory, save_name))
267267

268268
except IOError as e:
@@ -1191,6 +1191,36 @@ def zoom(self, probability, min_factor, max_factor):
11911191
else:
11921192
self.add_operation(Zoom(probability=probability, min_factor=min_factor, max_factor=max_factor))
11931193

1194+
def zoomout(self, probability, min_factor, max_factor):
1195+
"""
1196+
Zoom in to an image, while **maintaining its size**. The amount by
1197+
which the image is zoomed is a randomly chosen value between
1198+
:attr:`min_factor` and :attr:`max_factor`.
1199+
1200+
Typical values may be ``min_factor=1.1`` and ``max_factor=1.5``.
1201+
1202+
To zoom by a constant amount, set :attr:`min_factor` and
1203+
:attr:`max_factor` to the same value.
1204+
1205+
.. seealso:: See :func:`zoom_random` for zooming into random areas
1206+
of the image.
1207+
1208+
:param probability: A value between 0 and 1 representing the
1209+
probability that the operation should be performed.
1210+
:param min_factor: The minimum factor by which to zoom the image.
1211+
:param max_factor: The maximum factor by which to zoom the image.
1212+
:type probability: Float
1213+
:type min_factor: Float
1214+
:type max_factor: Float
1215+
:return: None
1216+
"""
1217+
if not 0 < probability <= 1:
1218+
raise ValueError(Pipeline._probability_error_text)
1219+
elif (min_factor <= 1) or (max_factor <=1):
1220+
raise ValueError("The min_factor or max_factor argument must be greater than 1.")
1221+
else:
1222+
self.add_operation(ZoomOut(probability=probability, min_factor=min_factor, max_factor=max_factor))
1223+
11941224
def zoom_random(self, probability, percentage_area, randomise_percentage_area=False):
11951225
"""
11961226
Zooms into an image at a random location within the image.

0 commit comments

Comments
 (0)