|
| 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() |
0 commit comments