Skip to content

Added sepia tone #1877

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 26, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions digital_image_processing/sepia.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""
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 value if value <= 255 else 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
img = imread("image_data/lena.jpg", 1)
img1 = imread("image_data/lena.jpg", 1)
img2 = imread("image_data/lena.jpg", 1)
img3 = imread("image_data/lena.jpg", 1)

# convert with sepia with different factor's value
sepia_10 = make_sepia(img, 10)
sepia_20 = make_sepia(img1, 20)
sepia_30 = make_sepia(img2, 30)
sepia_40 = make_sepia(img3, 40)

# show result images
imshow("Original image with sepia (factor: 10)", img)
imshow("Original image with sepia (factor: 20)", img1)
imshow("Original image with sepia (factor: 30)", img2)
imshow("Original image with sepia (factor: 40)", img3)
waitKey(0)
destroyAllWindows()
6 changes: 6 additions & 0 deletions digital_image_processing/test_digital_image_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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
Expand Down Expand Up @@ -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()