Skip to content

Commit 632891e

Browse files
committed
Add sepia tone
1 parent f6ee518 commit 632891e

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

digital_image_processing/sepia.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"""
2+
Implemented an algorithm using opencv to tone an image with sepia technic
3+
"""
4+
5+
from cv2 import imread, imshow, waitKey, destroyAllWindows
6+
7+
8+
def make_sepia(img, factor: int):
9+
pixel_h, pixel_v = img.shape[0], img.shape[1]
10+
11+
def to_grayscale(blue, green, red):
12+
"""
13+
Helper function to create pixel's greyscale representation
14+
Src: https://pl.wikipedia.org/wiki/YUV
15+
"""
16+
return 0.2126 * red + 0.587 * green + 0.114 * blue
17+
18+
def normalize(value):
19+
""" Helper function to normalize R/G/B value -> return 255 if value > 255"""
20+
return value if value <= 255 else 255
21+
22+
for i in range(pixel_h):
23+
for j in range(pixel_v):
24+
greyscale = int(to_grayscale(*img[i][j]))
25+
img[i][j] = [
26+
normalize(greyscale),
27+
normalize(greyscale + factor),
28+
normalize(greyscale + 2 * factor),
29+
]
30+
31+
return img
32+
33+
34+
if __name__ == "__main__":
35+
# read original image
36+
img = imread("image_data/lena.jpg", 1)
37+
img1 = imread("image_data/lena.jpg", 1)
38+
img2 = imread("image_data/lena.jpg", 1)
39+
img3 = imread("image_data/lena.jpg", 1)
40+
41+
# convert with sepia with different factor's value
42+
sepia_10 = make_sepia(img, 10)
43+
sepia_20 = make_sepia(img1, 20)
44+
sepia_30 = make_sepia(img2, 30)
45+
sepia_40 = make_sepia(img3, 40)
46+
47+
# show result images
48+
imshow("Original image with sepia (factor: 10)", img)
49+
imshow("Original image with sepia (factor: 20)", img1)
50+
imshow("Original image with sepia (factor: 30)", img2)
51+
imshow("Original image with sepia (factor: 40)", img3)
52+
waitKey(0)
53+
destroyAllWindows()

0 commit comments

Comments
 (0)