簡體   English   中英

python function 更改圖像的 RGB 值

[英]python function to change RGB values of an image

我有這個 function 只顯示圖像的紅色 RGB 值。 但是,我對 function 的工作原理感到困惑。 具體來說,為什么這行括號中有一個 0:

newimage[i][j] = [image[i][j][0], 0, 0]

如何更改此 function 以交換綠色和藍色 RGB 值,同時保持紅色值相同?

from matplotlib.pyplot import imshow #command to display a 2D array as an image
from imageio import imread #command to read images from a file or url

coffee = imread('imageio:coffee.png') #Image

def RedImage(image):
    newimage = ArrayOfZeros(400, 600)
    for i in range(400):
        for j in range(600):
            newimage[i][j] = [image[i][j][0], 0, 0]
    return newimage

imshow(RedImage(coffee))

newimage[i][j] = [image[i][j][0], 0, 0]

image[i][j]newimage[i][j]大概是 arrays,由圖像中特定像素位置(i, j)處的紅色、綠色和藍色三個值組成。

所以image[i][j][0]是原始圖像的紅色值,它也將用作新圖像的紅色值。 新圖像的藍色和綠色值將為 0。

原始圖像的綠色值為image[i][j][1] ,藍色值為image[i][j][2] 所以為了在新圖像中交換它們,使用舊圖像的綠色值在新圖像的藍色值的 position 中,舊圖像的藍色值在新圖像的綠色值的 position 中.

newimage[i][j] = [image[i][j][0], image[i][j][2], image[i][j][1]]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM