Here are two ways to do that in Python/OpenCV. Both are based upon the ratio of the log(mid-gray)/log(mean). Results are often reasonable, especially for dark image, but do not work in all cases. For bright image, invert the gray or value image, process as for dark images, then invert again and recombine if using the value image.

  • Read the input
  • Convert to gray or HSV value
  • Compute the ratio log(mid-gray)/log(mean) on the gray or value channel
  • Raise the input or value to the power of the ratio
  • If using the value channel, combine the new value channel with the hue and saturation channels and convert back to RGB

Input:

import cv2
import numpy as np
import math

# read image
img = cv2.imread('lioncuddle1.jpg')

# METHOD 1: RGB

# convert img to gray
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# compute gamma = log(mid*255)/log(mean)
mid = 0.5
mean = np.mean(gray)
gamma = math.log(mid*255)/math.log(mean)
print(gamma)

# do gamma correction
img_gamma1 = np.power(img, gamma).clip(0,255).astype(np.uint8)



# METHOD 2: HSV (or other color spaces)

# convert img to HSV
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
hue, sat, val = cv2.split(hsv)

# compute gamma = log(mid*255)/log(mean)
mid = 0.5
mean = np.mean(val)
gamma = math.log(mid*255)/math.log(mean)
print(gamma)

# do gamma correction on value channel
val_gamma = np.power(val, gamma).clip(0,255).astype(np.uint8)

# combine new value channel with original hue and sat channels
hsv_gamma = cv2.merge([hue, sat, val_gamma])
img_gamma2 = cv2.cvtColor(hsv_gamma, cv2.COLOR_HSV2BGR)

# show results
cv2.imshow('input', img)
cv2.imshow('result1', img_gamma1)
cv2.imshow('result2', img_gamma2)
cv2.waitKey(0)
cv2.destroyAllWindows()

# save results
cv2.imwrite('lioncuddle1_gamma1.jpg', img_gamma1)
cv2.imwrite('lioncuddle1_gamma2.jpg', img_gamma2)


Result from method 1:

Result from method 2:

Answer from fmw42 on Stack Overflow
🌐
PyImageSearch
pyimagesearch.com › home › blog › opencv gamma correction
OpenCV Gamma Correction - PyImageSearch
April 22, 2021 - There are two (easy) ways to apply gamma correction using OpenCV and Python. The first method is to simply leverage the fact that Python + OpenCV represents images as NumPy arrays.
🌐
OpenCV
docs.opencv.org › 3.4.20 › d3 › dc1 › tutorial_basic_linear_transform.html
OpenCV: Changing the contrast and brightness of an image!
After \( \alpha \), \( \beta \) ... in the right. After gamma correction, the histogram is shifted to the right but the pixels in the dark regions are more shifted (see the gamma curves figure) than those in the bright regions....
🌐
Lindevs
lindevs.com › apply-gamma-correction-to-an-image-using-opencv
Apply Gamma Correction to an Image using OpenCV | Lindevs
November 2, 2022 - import cv2 import numpy as np def gammaCorrection(src, gamma): invGamma = 1 / gamma table = [((i / 255) ** invGamma) * 255 for i in range(256)] table = np.array(table, np.uint8) return cv2.LUT(src, table) img = cv2.imread('test.jpg') gammaImg = gammaCorrection(img, 2.2) cv2.imshow('Original image', img) cv2.imshow('Gamma corrected image', gammaImg) cv2.waitKey(0) cv2.destroyAllWindows() #include <opencv2/opencv.hpp> using namespace cv; void gammaCorrection(const Mat &src, Mat &dst, const float gamma) { float invGamma = 1 / gamma; Mat table(1, 256, CV_8U); uchar *p = table.ptr(); for (int i = 0
🌐
Dynamsoft
dynamsoft.com › codepool › image-processing-opencv-gamma-correction.html
How to Use Gamma Correction for Image Processing with Opencv | Dynamsoft Developers Blog
November 9, 2015 - We can see when the image has low brightnesses and low contrast, we can process it by Gamma Correction, and the value of gamma should be less than 1.Because the algorithm can expand low gray steps and compress the high gray steps when γ < 1.
🌐
Medium
medium.com › @daniel.lepold › image-processing-in-python-gamma-correction-homography-transformation-and-face-detection-with-7c806a587186
Face Detection from ID Cards in Python using OpenCV: Gamma Correction & Homography | by Daniel Lepold | Medium
May 6, 2025 - Gamma correction is a method that allows nonlinear adjustment of an image’s brightness. The adjust_gamma() function applies gamma correction using a precomputed lookup table. A gamma value > 1 makes the image brighter, while a gamma value ...
Top answer
1 of 1
14

Here are two ways to do that in Python/OpenCV. Both are based upon the ratio of the log(mid-gray)/log(mean). Results are often reasonable, especially for dark image, but do not work in all cases. For bright image, invert the gray or value image, process as for dark images, then invert again and recombine if using the value image.

  • Read the input
  • Convert to gray or HSV value
  • Compute the ratio log(mid-gray)/log(mean) on the gray or value channel
  • Raise the input or value to the power of the ratio
  • If using the value channel, combine the new value channel with the hue and saturation channels and convert back to RGB

Input:

import cv2
import numpy as np
import math

# read image
img = cv2.imread('lioncuddle1.jpg')

# METHOD 1: RGB

# convert img to gray
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# compute gamma = log(mid*255)/log(mean)
mid = 0.5
mean = np.mean(gray)
gamma = math.log(mid*255)/math.log(mean)
print(gamma)

# do gamma correction
img_gamma1 = np.power(img, gamma).clip(0,255).astype(np.uint8)



# METHOD 2: HSV (or other color spaces)

# convert img to HSV
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
hue, sat, val = cv2.split(hsv)

# compute gamma = log(mid*255)/log(mean)
mid = 0.5
mean = np.mean(val)
gamma = math.log(mid*255)/math.log(mean)
print(gamma)

# do gamma correction on value channel
val_gamma = np.power(val, gamma).clip(0,255).astype(np.uint8)

# combine new value channel with original hue and sat channels
hsv_gamma = cv2.merge([hue, sat, val_gamma])
img_gamma2 = cv2.cvtColor(hsv_gamma, cv2.COLOR_HSV2BGR)

# show results
cv2.imshow('input', img)
cv2.imshow('result1', img_gamma1)
cv2.imshow('result2', img_gamma2)
cv2.waitKey(0)
cv2.destroyAllWindows()

# save results
cv2.imwrite('lioncuddle1_gamma1.jpg', img_gamma1)
cv2.imwrite('lioncuddle1_gamma2.jpg', img_gamma2)


Result from method 1:

Result from method 2:

🌐
GitHub
github.com › GeorgeSeif › Image-Processing-OpenCV › blob › master › Gamma Correction and White Balance › enhance.cpp
Image-Processing-OpenCV/Gamma Correction and White Balance/enhance.cpp at master · GeorgeSeif/Image-Processing-OpenCV
Mat gamma_correction(Mat img, float gamma=0) { CV_Assert(img.data); · // Accept only char type matrices · CV_Assert(img.depth() != sizeof(uchar)); · · if (gamma == 0) {gamma = auto_gamma_value(img);} · · // Build look up table ·
🌐
DEV Community
dev.to › tinazhouhui › discovering-opencv-with-python-gamma-correction-3cnh
Discovering OpenCV with Python: Gamma correction - DEV Community
January 11, 2023 - Another practical exercise that I had quite some fun with was gamma correction. This concept is mainly used when we want to adjust the brightness of an image. We could also use it to restore the faded pictures to their previous depth of colour.
Find elsewhere
🌐
GitHub
github.com › leowang7 › iagcwd
GitHub - leowang7/iagcwd: An improved contrast enhancement method for image. :camera: · GitHub
This is an python implementation of the paper "Contrast enhancement of brightness-distorted images by improved adaptive gamma correction." at https://arxiv.org/abs/1709.04427. The purpose of the algorithm is to improve the contrast of the image adaptively. Ubuntu 18.04.1 LTS · Python >= 3.6 · Opencv >= 3.4.1 ·
Starred by 69 users
Forked by 18 users
Languages: Python
🌐
YouTube
youtube.com › watch
OpenCV 13: Gamma Correction to Increase or Decrease Brightness | Python | OpenCV - YouTube
OpenCV 13: Gamma Correction to Increase or Decrease Brightness | Python | OpenCVGitHub JupyterNotebook: https://github.com/siddiquiamir/OpenCVGitHub Data: ht...
Published: August 21, 2023
🌐
Medium
medium.com › @pontus.bergmark › intro-to-image-processing-in-opencv-with-python-1c7f94af18a7
Intro to Image Processing in OpenCV with Python | by Pontus Bergmark | Medium
October 11, 2018 - The above transformation uses r ... we have different results. So the gamma correction is the process of choosing the best value for gamma to have the best output image....
🌐
OpenCV
docs.opencv.org › 4.x › dc › dfe › group__intensity__transform.html
OpenCV: The module brings implementations of intensity transformation algorithms to adjust image contrast.
Given an input bgr or grayscale image and constant gamma, apply power-law transformation, a.k.a. gamma correction to the image on domain [0, 255] and return the resulting image.
🌐
OpenCV
forum.opencv.org › python
Auto selection of Gamma value for image brightness - Python - OpenCV
January 19, 2021 - I have a sequence of images or live video and I want to check if the image is darker or brighter, then adjust the brightness using gamma correction i.e. Image ^ gamma. gamma = 1 do nothing and smaller than 1 value of ga…
🌐
GitHub
github.com › atapour › ip-python-opencv › blob › main › gamma_correction.py
ip-python-opencv/gamma_correction.py at main · atapour/ip-python-opencv
Examples and code demonstrations for the Image Processing module at Durham University - ip-python-opencv/gamma_correction.py at main · atapour/ip-python-opencv
Author: atapour
🌐
GitHub
github.com › ParanoidAndrea › Gamma_Correction
GitHub - ParanoidAndrea/Gamma_Correction: C++ OpenCV gamma correction functions · GitHub
It performs gamma correction on an image to adjust its brightness and contrast levels. The code includes the necessary headers to use OpenCV, as well as the standard input/output library and the cmath library for mathematical operations.
Author: ParanoidAndrea
🌐
Medium
medium.com › @sinhaaa003 › transforming-images-rgb-to-grayscale-and-gamma-correction-with-python-fe5a0afa12b9
Transforming Images: RGB to Grayscale and Gamma Correction with Python | by Aditya Sinha | Medium
July 30, 2024 - Opencv · Aditya Sinha · 4 min read · ·Jul 30, 2024 · -- Listen · Share · By Aditya Sinha and Bhumikamohan · Welcome to the fascinating world of image processing! In this article, we, Aditya Sinha and Bhumika M, will guide you through two fundamental yet powerful transformations: converting an RGB image to grayscale and applying gamma correction.
🌐
OpenCV
forum.opencv.org › uncategorized
Improvement in HDR Tutorial – Clarifying Gamma Correction (Fix for Issue #22219) - Uncategorized - OpenCV
October 8, 2025 - Hello everyone, My name is Jay Pol, a C++ and Python developer passionate about computer vision and OpenCV. I recently explored the HDR imaging tutorials and noticed that the gamma correction step could be confusing for new users — especially when comparing the results with default tone mapping values.
🌐
OpenCV Q&A Forum
answers.opencv.org › question › 71577 › how-to-determine-if-gamma-decorrection-is-required
How to determine if gamma decorrection is required? - OpenCV Q&A Forum
September 24, 2015 - I've read that OpenCV does not enforce its own version of the RBG color space (say sRGB or AdobeRGB) - this depends on the image capture device and/or image file format and imread will simply load the same values in. I have an application wherein I need to train classifiers based on color image intensity values, and am facing the following doubt: If the image is in sRGB, it has been gamma corrected (with say gamma = 2.2) and therefore I need to de-correct it with gamma set to 1/2.2 in order to convert to a linear RGB colorspace and perform operations on it.