![](/img/trans.png)
[英]How can i generate an image determining the color of each pixel in python?
[英]How can I create a color image (RGB), after changing it into a 3 dimensional array of each pixel?
我的问题是,我正在拍摄样本图像,并尝试更改特定的RGB值以更改外观。 获取每个像素的数据后,将其保存为3维数组,然后尝试将其转换回图像,但是出现以下错误。
我知道它想要二维数组将其转换回图像,但是我不知道如何保持颜色不变并将其转换回图像。 有任何想法吗?
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()
谢谢
幸运的是,这里的解决方案非常简单。
代替
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
。
看看这出更多的模式。
希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.