mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
Added sepia tone (#1877)
* Add sepia tone * Add unit test * technic --> technique * Update digital_image_processing/sepia.py Co-Authored-By: Christian Clauss <cclauss@me.com> * Update digital_image_processing/sepia.py Co-Authored-By: Christian Clauss <cclauss@me.com> * Fixed errors after commit changes * Fixed errors Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
parent
e5dd2b1eb7
commit
5933cd4d83
48
digital_image_processing/sepia.py
Normal file
48
digital_image_processing/sepia.py
Normal file
|
@ -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()
|
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue
Block a user