写一个对image文件夹中所有的图片进行gamma变换的python算法
时间: 2024-05-08 22:18:09 浏览: 95
好的,下面是一个简单的对image文件夹中所有的图片进行gamma变换的Python算法示例:
```python
import os
import cv2
import numpy as np
# Gamma correction function
def gamma_correction(img, gamma):
inv_gamma = 1.0 / gamma
table = np.array([((i/255.0) ** inv_gamma) * 255
for i in np.arange(0, 256)]).astype("uint8")
return cv2.LUT(img, table)
# Path to the image folder
img_folder = 'image/'
# Loop over all the images in the folder
for filename in os.listdir(img_folder):
if filename.endswith('.jpg') or filename.endswith('.png'):
# Load the image and apply gamma correction
img_path = os.path.join(img_folder, filename)
img = cv2.imread(img_path)
img_gamma = gamma_correction(img, 0.5) # Change gamma value as needed
# Save the corrected image with '_gamma' suffix
img_name, img_ext = os.path.splitext(filename)
img_gamma_path = img_name + '_gamma' + img_ext
cv2.imwrite(os.path.join(img_folder, img_gamma_path), img_gamma)
```
这段代码会对'./image/'文件夹中所有的.jpg和.png图片应用Gamma校正,并将结果保存在文件名上加上'_gamma'的同目录下。注意:Gamma值可以根据需要进行更改。
阅读全文