diff --git a/digital_image_processing/sepia.py b/digital_image_processing/sepia.py new file mode 100644 index 000000000..e91d57d03 --- /dev/null +++ b/digital_image_processing/sepia.py @@ -0,0 +1,48 @@ +""" + Implemented an algorithm using opencv to tone an image with sepia technique +""" + +from cv2 import imread, imshow, waitKey, destroyAllWindows + + +def make_sepia(img, factor: int): + """ Function create sepia tone. Source: https://en.wikipedia.org/wiki/Sepia_(color) """ + pixel_h, pixel_v = img.shape[0], img.shape[1] + + def to_grayscale(blue, green, red): + """ + Helper function to create pixel's greyscale representation + Src: https://pl.wikipedia.org/wiki/YUV + """ + return 0.2126 * red + 0.587 * green + 0.114 * blue + + def normalize(value): + """ Helper function to normalize R/G/B value -> return 255 if value > 255""" + return min(value, 255) + + for i in range(pixel_h): + for j in range(pixel_v): + greyscale = int(to_grayscale(*img[i][j])) + img[i][j] = [ + normalize(greyscale), + normalize(greyscale + factor), + normalize(greyscale + 2 * factor), + ] + + return img + + +if __name__ == "__main__": + # read original image + images = { + percentage: imread("image_data/lena.jpg", 1) for percentage in (10, 20, 30, 40) + } + + for percentage, img in images.items(): + make_sepia(img, percentage) + + for percentage, img in images.items(): + imshow(f"Original image with sepia (factor: {percentage})", img) + + waitKey(0) + destroyAllWindows() diff --git a/digital_image_processing/test_digital_image_processing.py b/digital_image_processing/test_digital_image_processing.py index b9a721110..5c6127337 100644 --- a/digital_image_processing/test_digital_image_processing.py +++ b/digital_image_processing/test_digital_image_processing.py @@ -9,6 +9,7 @@ import digital_image_processing.filters.sobel_filter as sob import digital_image_processing.filters.convolve as conv import digital_image_processing.change_contrast as cc import digital_image_processing.convert_to_negative as cn +import digital_image_processing.sepia as sp from cv2 import imread, cvtColor, COLOR_BGR2GRAY from numpy import array, uint8 from PIL import Image @@ -68,3 +69,8 @@ def test_median_filter(): def test_sobel_filter(): grad, theta = sob.sobel_filter(gray) assert grad.any() and theta.any() + + +def test_sepia(): + sepia = sp.make_sepia(img, 20) + assert sepia.all()