I try to manually adjust the contrast of the image. The 2nd code works fine with me but it uses loop -> it's slow so I change to 1st code but get a different output.
I don't know what gone wrong because I assumed that these 2 are alike. Can somebody tell me what did I miss? Thanks a lot.
Code with no-loop
from PIL import Image
# level should be in range of -255 to +255 to decrease or increase contrast
def change_contrast(img, level):
    def truncate(v):
        v[v<0] = 0
        v[v>255] = 255
        return v
    img_c = np.reshape(img,(1,img.shape[0] *img.shape[1],3))[0].copy()
   
    
    factor = (259 * (level+255)) / (255 * (259-level))
    img_c = factor * (img_c-128) + 128
    img_c = truncate(img_c)
    return np.reshape(img_c,(img.shape[0] ,img.shape[1],3)).astype(np.uint8)
Code with loop
from PIL import Image
# level should be in range of -255 to +255 to decrease or increase contrast
def change_contrast(img, level):
    def truncate(v):
        v[v<0] = 0
        v[v>255] = 255
        return v
    img_c = np.reshape(img,(1,img.shape[0] *img.shape[1],3))[0].copy()
   
    factor = (259 * (level+255)) / (255 * (259-level))
    for x in range(img_c.shape[0]):
        color = img_c[x]
        new_color = truncate(np.asarray([int(factor * (c-128) + 128) for c in color]))
        img_c[x]=     new_color
    return np.reshape(img_c,(img.shape[0] ,img.shape[1],3))
Test code
a = PIL.Image.open('Test.jpg')
b = np.asarray(a)
Image.fromarray(change_contrast(b , 128))
Result:
non_loop:
loop:
Solution: my original array has uint8 type which won't let you have negative numbers (ex: img_c - 128). Change to int should help!
Gotta check type more carefully.


