sepia.py 748 B

12345678910111213141516171819202122
  1. import cv2
  2. import numpy as np
  3. def sepia(img):
  4. res = img.copy()
  5. res = cv2.cvtColor(res, cv2.COLOR_BGR2RGB) # converting to RGB as sepia matrix is for RGB
  6. res = np.array(res, dtype=np.float64)
  7. res = cv2.transform(res, np.matrix([[0.393, 0.769, 0.189],
  8. [0.349, 0.686, 0.168],
  9. [0.272, 0.534, 0.131]]))
  10. res[np.where(res > 255)] = 255 # clipping values greater than 255 to 255
  11. res = np.array(res, dtype=np.uint8)
  12. res = cv2.cvtColor(res, cv2.COLOR_RGB2BGR)
  13. cv2.imshow("original", img)
  14. cv2.imshow("Sepia", res)
  15. cv2.waitKey(0)
  16. cv2.destroyAllWindows()
  17. if __name__ == "__main__":
  18. img = cv2.imread("image.jpg")
  19. sepia(img)