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 all 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
48 changes: 48 additions & 0 deletions digital_image_processing/sepia.py
Original file line number Diff line number Diff line change
@@ -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()
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()