Search code examples
pythonimage-processingcolors

How to change hair color to purple?


Is there something wrong with my code? I would like to change the hair color of the picture below from green to purple. However, my output looks weird. I'd appreciate if someone experienced with computer vision/image processing could help me out with this.

from scipy import misc,ndimage
import matplotlib.pyplot as plt
import numpy as np

def dye_hair(filename):
    pic = misc.imread(filename)
    output = []
    for i in range(len(pic)):
        for j in pic[i]:
            if (j[1] > j[0]) & (j[1] > j[2]):
                pic[i][j] = [j[0]*2,j[1]*0.2,j[2]*0.8]
plt.imshow(pic)
plt.show()
        

The logic is this, if pixel green value is more than red and blue value, replace the color with [R*2, G*0.2. B*0.8].

green-haired-girl


Solution

  • It is because you are not iterating over all the columns. You should do it for j in range(len(pic[i])) try with this one:

    def dye_hair(filename):
        pic = misc.imread(filename)
        output = []
        for i in range(len(pic)):
            for j in range(len(pic[i])):
                if (pic[i][j][1] > pic[i][j][0]) and (pic[i][j][1] > pic[i][j][2]):
                    pic[i][j] = [pic[i][j][0]*2,pic[i][j][1]*0.2,pic[i][j][2]*0.8]
        plt.imshow(pic)
        plt.show()