Skip to content

Commit

Permalink
add random change image brightness color and contrast
Browse files Browse the repository at this point in the history
  • Loading branch information
CarkusL committed May 7, 2018
1 parent 1620421 commit 3642e33
Show file tree
Hide file tree
Showing 3 changed files with 307 additions and 1 deletion.
150 changes: 149 additions & 1 deletion Augmentor/Operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
print_function, unicode_literals)
from builtins import *

from PIL import Image, ImageOps
from PIL import Image, ImageOps, ImageEnhance
import math
from math import floor, ceil

Expand Down Expand Up @@ -265,6 +265,154 @@ def do(image):

return augmented_images

class RandomBrightness(Operation):
"""
This class is used to random change image brightness.
"""
def __init__(self, probability, min_factor,max_factor):
"""
required :attr:`probability` parameter
:func:`~Augmentor.Pipeline.Pipeline.random_brightness` function.
:param probability: Controls the probability that the operation is
performed when it is invoked in the pipeline.
:param min_factor: The value between 0.0 and max_factor
that define the minimum adjustment of image brightness.
The value 0.0 gives a black image,The value 1.0 gives the original image, value bigger than 1.0 gives more bright image.
:param max_factor: A value should be bigger than min_factor.
that define the maximum adjustment of image brightness.
The value 0.0 gives a black image, value 1.0 gives the original image, value bigger than 1.0 gives more bright image.
:type probability: Float
:type max_factor: Float
:type max_factor: Float
"""
Operation.__init__(self, probability)
self.min_factor = min_factor
self.max_factor = max_factor
def perform_operation(self, images):
"""
Random change the passed image brightness.
:param images: The image to convert into monochrome.
:type images: List containing PIL.Image object(s).
:return: The transformed image(s) as a list of object(s) of type
PIL.Image.
"""

def do(image):

factor = np.random.uniform(self.min_factor, self.max_factor)
imgenhancer_Brightness = ImageEnhance.Brightness(image)
return imgenhancer_Brightness.enhance(factor)

augmented_images = []

for image in images:
augmented_images.append(do(image))

return augmented_images

class RandomColor(Operation):
"""
This class is used to random change saturation of an image.
"""
def __init__(self, probability, min_factor,max_factor):
"""
required :attr:`probability` parameter
:func:`~Augmentor.Pipeline.Pipeline.random_color` function.
:param probability: Controls the probability that the operation is
performed when it is invoked in the pipeline.
:param min_factor: The value between 0.0 and max_factor
that define the minimum adjustment of image saturation.
The value 0.0 gives a black and white image, value 1.0 gives the original image.
:param max_factor: A value should be bigger than min_factor.
that define the maximum adjustment of image saturation.
The value 0.0 gives a black and white image, value 1.0 gives the original image.
:type probability: Float
:type max_factor: Float
:type max_factor: Float
"""
Operation.__init__(self, probability)
self.min_factor = min_factor
self.max_factor = max_factor
def perform_operation(self, images):
"""
Random change the passed image saturation.
:param images: The image to convert into monochrome.
:type images: List containing PIL.Image object(s).
:return: The transformed image(s) as a list of object(s) of type
PIL.Image.
"""

def do(image):

factor = np.random.uniform(self.min_factor, self.max_factor)
imgenhancer_Color = ImageEnhance.Color(image)
return imgenhancer_Color.enhance(factor)

augmented_images = []

for image in images:
augmented_images.append(do(image))

return augmented_images


class RandomContrast(Operation):
"""
This class is used to random change contrast of an image.
"""
def __init__(self, probability, min_factor,max_factor):
"""
required :attr:`probability` parameter
:func:`~Augmentor.Pipeline.Pipeline.random_contrast` function.
:param probability: Controls the probability that the operation is
performed when it is invoked in the pipeline.
:param min_factor: The value between 0.0 and max_factor
that define the minimum adjustment of image contrast.
The value 0.0 gives s solid grey image, value 1.0 gives the original image.
:param max_factor: A value should be bigger than min_factor.
that define the maximum adjustment of image contrast.
The value 0.0 gives s solid grey image, value 1.0 gives the original image.
:type probability: Float
:type max_factor: Float
:type max_factor: Float
"""
Operation.__init__(self, probability)
self.min_factor = min_factor
self.max_factor = max_factor
def perform_operation(self, images):
"""
Random change the passed image contrast.
:param images: The image to convert into monochrome.
:type images: List containing PIL.Image object(s).
:return: The transformed image(s) as a list of object(s) of type
PIL.Image.
"""

def do(image):

factor = np.random.uniform(self.min_factor, self.max_factor)
imgenhancer_Contrast = ImageEnhance.Contrast(image)
return imgenhancer_Contrast.enhance(factor)

augmented_images = []

for image in images:
augmented_images.append(do(image))

return augmented_images


class Skew(Operation):
"""
Expand Down
63 changes: 63 additions & 0 deletions Augmentor/Pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -1478,6 +1478,69 @@ def invert(self, probability):
else:
self.add_operation(Invert(probability=probability))

def random_brightness(self,probability,min_factor,max_factor):
"""
Random change brightness of an image.
:param probability: A value between 0 and 1 representing the
probability that the operation should be performed.
:param min_factor: The value between 0.0 and max_factor that define the minimum adjustment of image brightness.
The value 0.0 gives a black image, value 1.0 gives the original image, value bigger than 1.0 gives more bright image.
:param max_factor: A value should be bigger than min_factor that define the maximum adjustment of image brightness.
The value 0.0 gives a black image, value 1.0 gives the original image, value bigger than 1.0 gives more bright image.
:return: None
"""
if not 0 < probability <= 1:
raise ValueError(Pipeline._probability_error_text)
elif not 0 <= min_factor <= max_factor:
raise ValueError("The min_factor must be between 0 and max_factor.")
elif not min_factor <= max_factor:
raise ValueError("The max_factor must be bigger min_factor.")
else:
self.add_operation(RandomBrightness(probability=probability, min_factor=min_factor,max_factor=max_factor))

def random_color(self,probability,min_factor,max_factor):
"""
Random change saturation of an image.
:param probability: Controls the probability that the operation is
performed when it is invoked in the pipeline.
:param min_factor: The value between 0.0 and max_factor that define the minimum adjustment of image saturation.
The value 0.0 gives a black and white image, value 1.0 gives the original image.
:param max_factor: A value should be bigger than min_factor that define the maximum adjustment of image saturation.
The value 0.0 gives a black and white image, value 1.0 gives the original image.
:return: None
"""
if not 0 < probability <= 1:
raise ValueError(Pipeline._probability_error_text)
elif not 0 <= min_factor <= max_factor:
raise ValueError("The min_factor must be between 0 and max_factor.")
elif not min_factor <= max_factor:
raise ValueError("The max_factor must be bigger min_factor.")
else:
self.add_operation(RandomColor(probability=probability, min_factor=min_factor,max_factor=max_factor))

def random_contrast(self,probability,min_factor,max_factor):
"""
Random change image contrast.
:param probability: Controls the probability that the operation is
performed when it is invoked in the pipeline.
:param min_factor: The value between 0.0 and max_factor that define the minimum adjustment of image contrast.
The value 0.0 gives s solid grey image, value 1.0 gives the original image.
:param max_factor: A value should be bigger than min_factor that define the maximum adjustment of image contrast.
The value 0.0 gives s solid grey image, value 1.0 gives the original image.
:return: None
"""
if not 0 < probability <= 1:
raise ValueError(Pipeline._probability_error_text)
elif not 0 <= min_factor <= max_factor:
raise ValueError("The min_factor must be between 0 and max_factor.")
elif not min_factor <= max_factor:
raise ValueError("The max_factor must be bigger min_factor.")
else:
self.add_operation(RandomContrast(probability=probability, min_factor=min_factor,max_factor=max_factor))

def random_erasing(self, probability, rectangle_area):
"""
Work in progress. This operation performs a Random Erasing operation,
Expand Down
95 changes: 95 additions & 0 deletions tests/test_random_color_brightness_contrast.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import os
import sys
sys.path.insert(0, os.path.abspath('.'))

import tempfile
import shutil
from PIL import Image
from Augmentor import Operations


from util_funcs import create_colour_temp_image, create_greyscale_temp_image


def test_random_color_in_memory():

tmp, tmpdir = create_colour_temp_image((800, 800), "JPEG")

op = Operations.RandomColor(probability=1,min_factor=0.5, max_factor =1.5)
tmp_im = []
tmp_im.append(Image.open(tmp))
tmp_im = op.perform_operation(tmp_im)

assert tmp_im is not None
assert isinstance(tmp_im[0], Image.Image)


tmp_bw, tmpdir_bw = create_greyscale_temp_image((800, 800), "PNG")

op = Operations.RandomColor(probability=1,min_factor=0.5, max_factor =1.5)
tmp_im = []
tmp_im.append(Image.open(tmp))
tmp_im = op.perform_operation(tmp_im)

assert tmp_im is not None
assert isinstance(tmp_im[0], Image.Image)

tmp.close()
tmp_bw.close()
shutil.rmtree(tmpdir)
shutil.rmtree(tmpdir_bw)

def test_random_contrast_in_memory():

tmp, tmpdir = create_colour_temp_image((800, 800), "JPEG")

op = Operations.RandomContrast(probability=1,min_factor=0.5, max_factor =1.5)
tmp_im = []
tmp_im.append(Image.open(tmp))
tmp_im = op.perform_operation(tmp_im)

assert tmp_im is not None
assert isinstance(tmp_im[0], Image.Image)


tmp_bw, tmpdir_bw = create_greyscale_temp_image((800, 800), "PNG")

op = Operations.RandomContrast(probability=1,min_factor=0.5, max_factor =1.5)
tmp_im = []
tmp_im.append(Image.open(tmp))
tmp_im = op.perform_operation(tmp_im)

assert tmp_im is not None
assert isinstance(tmp_im[0], Image.Image)

tmp.close()
tmp_bw.close()
shutil.rmtree(tmpdir)
shutil.rmtree(tmpdir_bw)

def test_random_brightness_in_memory():

tmp, tmpdir = create_colour_temp_image((800, 800), "JPEG")

op = Operations.RandomBrightness(probability=1,min_factor=0.5, max_factor =1.5)
tmp_im = []
tmp_im.append(Image.open(tmp))
tmp_im = op.perform_operation(tmp_im)

assert tmp_im is not None
assert isinstance(tmp_im[0], Image.Image)

tmp_bw, tmpdir_bw = create_greyscale_temp_image((800, 800), "PNG")

op = Operations.RandomBrightness(probability=1,min_factor=0.5, max_factor =1.5)
tmp_im = []
tmp_im.append(Image.open(tmp))
tmp_im = op.perform_operation(tmp_im)

assert tmp_im is not None
assert isinstance(tmp_im[0], Image.Image)

tmp.close()
tmp_bw.close()
shutil.rmtree(tmpdir)
shutil.rmtree(tmpdir_bw)

0 comments on commit 3642e33

Please sign in to comment.