Reputation: 21
I am trying to create a function that will allow me to swap every red and blue pixel of an image. However, when running the function, new image does not change or do the intended. So far, I am only trying to change the image to only blue filter to test the function.
from CSE8AImage import *
img = load_img('images/cat.jpg')
def complement(img):
for r in range(len(img)):
for c in range(len(img[r])):
pix = img[r][c]
img[r][c] = (0, 0, pix[2])
return img
save_img(img, 'complement_cat.jpg')
Upvotes: 0
Views: 833
Reputation: 3283
What you're doing in your code is simply setting the red and green pixels to 0(assuming it's RGB? I couldn't find anything about the CSE8AImage library outside of this page which perfectly matches your question). I will continue assuming it's in RGB.
What you should change in your code to make it work is simply change img[r][c] = (0,0,pix[2])
to img[r][c] = pix[[2,1,0]]
as this is saying to reorder the pixels (RGB, index 0,1,2) to the new order (BGR, index 2,1,0).
A simpler way would just be to do the whole array at once:
def complement(img):
return img[:,:,[2,1,0]]
This will only work if you can index it like an array in python. Ignore this if this is not the case.
Upvotes: 2