我的问题是我正在拍摄样本图像并尝试更改特定的RGB值以更改外观。在获取每个像素的数据后,将其保存为三维数组,然后我尝试将其转换回图像,但是我收到以下错误。
我知道它想要二维数组将其转换回图像,但我不知道如何将其保持颜色并将其转换回图像。有什么想法吗?
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()
由于
答案 0 :(得分:0)
这里的解决方案很幸运。
而不是
new_img = Image.fromarray(np.uint8(img_as_list),"L")
使用
new_img = Image.fromarray(np.uint8(img_as_list))
L
意味着它需要8位像素,黑色和白色。因此每个像素只需要0-255的值。而是使用另一种模式。
不是完全删除参数,而是根据文件类型使用RGB
或RGBA
。
查看this以了解更多模式。
希望这有帮助!