Feature #904 » gamma_correction.py
1 |
import numpy as np |
---|---|
2 |
from PIL import Image |
3 |
|
4 |
def adjust_gamma_with_gradient(image_array, start_gamma, end_gamma): |
5 |
image_array = np.array(image_array, dtype='float32') |
6 |
width, height = image_array.shape[1], image_array.shape[0] |
7 |
|
8 |
gamma_range = np.linspace(start_gamma, end_gamma, width) |
9 |
inv_gamma_table = [1.0 / g for g in gamma_range] |
10 |
table = np.array([((i / 255.0) ** inv_gamma) * 255 for inv_gamma in inv_gamma_table for i in range(256)]) |
11 |
table = table.reshape((width, 256)) |
12 |
|
13 |
for i in range(width): |
14 |
image_array[:, i] = table[i, image_array[:, i].astype(int)] |
15 |
|
16 |
image_array = np.clip(image_array, 0, 255).astype('uint8') |
17 |
return image_array |