Skip to content

Commit 92281cc

Browse files
l3str4ngecclauss
authored andcommitted
Added sepia tone (TheAlgorithms#1877)
* Add sepia tone * Add unit test * technic --> technique * Update digital_image_processing/sepia.py Co-Authored-By: Christian Clauss <[email protected]> * Update digital_image_processing/sepia.py Co-Authored-By: Christian Clauss <[email protected]> * Fixed errors after commit changes * Fixed errors Co-authored-by: Christian Clauss <[email protected]>
1 parent 1f1afda commit 92281cc

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

digital_image_processing/sepia.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"""
2+
Implemented an algorithm using opencv to tone an image with sepia technique
3+
"""
4+
5+
from cv2 import imread, imshow, waitKey, destroyAllWindows
6+
7+
8+
def make_sepia(img, factor: int):
9+
""" Function create sepia tone. Source: https://en.wikipedia.org/wiki/Sepia_(color) """
10+
pixel_h, pixel_v = img.shape[0], img.shape[1]
11+
12+
def to_grayscale(blue, green, red):
13+
"""
14+
Helper function to create pixel's greyscale representation
15+
Src: https://pl.wikipedia.org/wiki/YUV
16+
"""
17+
return 0.2126 * red + 0.587 * green + 0.114 * blue
18+
19+
def normalize(value):
20+
""" Helper function to normalize R/G/B value -> return 255 if value > 255"""
21+
return min(value, 255)
22+
23+
for i in range(pixel_h):
24+
for j in range(pixel_v):
25+
greyscale = int(to_grayscale(*img[i][j]))
26+
img[i][j] = [
27+
normalize(greyscale),
28+
normalize(greyscale + factor),
29+
normalize(greyscale + 2 * factor),
30+
]
31+
32+
return img
33+
34+
35+
if __name__ == "__main__":
36+
# read original image
37+
images = {
38+
percentage: imread("image_data/lena.jpg", 1) for percentage in (10, 20, 30, 40)
39+
}
40+
41+
for percentage, img in images.items():
42+
make_sepia(img, percentage)
43+
44+
for percentage, img in images.items():
45+
imshow(f"Original image with sepia (factor: {percentage})", img)
46+
47+
waitKey(0)
48+
destroyAllWindows()

digital_image_processing/test_digital_image_processing.py

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import digital_image_processing.filters.convolve as conv
1010
import digital_image_processing.change_contrast as cc
1111
import digital_image_processing.convert_to_negative as cn
12+
import digital_image_processing.sepia as sp
1213
from cv2 import imread, cvtColor, COLOR_BGR2GRAY
1314
from numpy import array, uint8
1415
from PIL import Image
@@ -68,3 +69,8 @@ def test_median_filter():
6869
def test_sobel_filter():
6970
grad, theta = sob.sobel_filter(gray)
7071
assert grad.any() and theta.any()
72+
73+
74+
def test_sepia():
75+
sepia = sp.make_sepia(img, 20)
76+
assert sepia.all()

0 commit comments

Comments
 (0)