![](/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.