Skip to content

Instantly share code, notes, and snippets.

@yptheangel
Created September 28, 2021 11:22
Show Gist options
  • Save yptheangel/5cc521db739f15dadcf7d5ad4830a20c to your computer and use it in GitHub Desktop.
Save yptheangel/5cc521db739f15dadcf7d5ad4830a20c to your computer and use it in GitHub Desktop.
Change/Augment brightness of an image using OpenCV
import cv2
import numpy as np
def augment_brightness(img, scale):
img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
h, s, v = cv2.split(img)
scale = 0.5
v = np.clip(v * scale, 0, 255, out=v)
img = cv2.merge((h, s, v))
img = cv2.cvtColor(img, cv2.COLOR_HSV2BGR)
return img
if __name__ == '__main__':
IMG_PATH = r"C:\Users\WILSONCYP\Pictures\image.jpg"
frame = cv2.imread(IMG_PATH)
edited = augment_brightness(frame, 0.5) # increasing the second argument brightens, decreasing darkens
cv2.imshow("raw", frame)
cv2.imshow("edited", edited)
if cv2.waitKey(0) == 27:
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment