Python 在将彩色图像(RGB)更改为每个像素的三维阵列后,如何创建彩色图像(RGB)?
我的问题是,我正在拍摄一张样本图像,并试图更改特定的RGB值以更改外观。在获得每个像素的数据后,将其保存为三维数组,然后尝试将其转换回图像,但我得到以下错误 我知道它需要二维数组将其转换回图像,但我不知道如何保持颜色并将其转换回图像。有什么想法吗Python 在将彩色图像(RGB)更改为每个像素的三维阵列后,如何创建彩色图像(RGB)?,python,arrays,numpy,image-processing,python-imaging-library,Python,Arrays,Numpy,Image Processing,Python Imaging Library,我的问题是,我正在拍摄一张样本图像,并试图更改特定的RGB值以更改外观。在获得每个像素的数据后,将其保存为三维数组,然后尝试将其转换回图像,但我得到以下错误 我知道它需要二维数组将其转换回图像,但我不知道如何保持颜色并将其转换回图像。有什么想法吗 from PIL import Image import numpy as np # Opens the image, giving it the call name im img = Image.open("sample_images\sampl
from PIL import Image
import numpy as np
# Opens the image, giving it the call name im
img = Image.open("sample_images\sample_image_1.jpg")
# This function makes a list containing every RGB value of each pixel in sample_image_1
img_as_list = np.uint8(np.asarray(img))
print(img_as_list)
for x in img_as_list:
for rVal in x[0]:
if rVal <= 255:
rVal = 0
for bVal in x[1]:
if bVal <= 255:
bVal = 0
for gVal in x[2]:
if gVal <= 255:
gVal = 0
# Crate a new image from the list that i made from the picture
new_img = Image.fromarray(np.uint8(img_as_list),"L")
print(img_as_list)
new_img.show()
img.show()
从PIL导入图像
将numpy作为np导入
#打开图像,将其命名为im
img=Image.open(“sample\u images\sample\u Image\u 1.jpg”)
#此函数生成一个列表,其中包含采样图像1中每个像素的每个RGB值
img_as_list=np.uint8(np.asarray(img))
打印(img\U as\U列表)
对于img\U as\U列表中的x:
对于x[0]中的rVal:
如果rVal这里的解决方案很简单
而不是
new_img = Image.fromarray(np.uint8(img_as_list),"L")
使用
L
意味着它需要8位像素,黑色和白色。因此,每个像素只需要一个0-255的值。而是使用另一种模式
您可以根据文件类型使用RGB
或RGBA
,而不是完全删除参数
查看更多模式
希望这有帮助 将错误添加为文本,而不是图像
new_img = Image.fromarray(np.uint8(img_as_list))