python 用参数传递代替for循环 将1维数组修改成2维RGB图像, 实现RGB图像像素值整体操作 (如整体增强,整体转灰度值)
时间: 2024-06-05 15:06:13 浏览: 127
# 将一维数组转换为二维RGB图像
def convert_to_rgb(data, width, height):
rgb = [[(0, 0, 0)] * width for _ in range(height)]
for i in range(len(data)):
row = i // width
col = i % width
rgb[row][col] = (data[i], data[i], data[i])
return rgb
# 整体增强
def enhance_image(rgb, factor):
for row in range(len(rgb)):
for col in range(len(rgb[row])):
r = min(int(rgb[row][col][0] * factor), 255)
g = min(int(rgb[row][col][1] * factor), 255)
b = min(int(rgb[row][col][2] * factor), 255)
rgb[row][col] = (r, g, b)
return rgb
# 整体转灰度值
def convert_to_gray(rgb):
for row in range(len(rgb)):
for col in range(len(rgb[row])):
gray = int((rgb[row][col][0] + rgb[row][col][1] + rgb[row][col][2]) / 3)
rgb[row][col] = (gray, gray, gray)
return rgb
# 示例代码
data = [255, 0, 0, 0, 255, 0, 0, 0, 255]
width = 3
height = 3
rgb = convert_to_rgb(data, width, height)
print(rgb)
rgb = enhance_image(rgb, 1.5)
print(rgb)
rgb = convert_to_gray(rgb)
print(rgb)
阅读全文