Python - change pixel value
Question: I have used this code to change the pixel value of image,

    
    
    im=Image.open("image.jpg")
    pix=im.load()
    pix[50,50]=(70,70,70)
    

it is work well with me, but when I use a png image everything is okay and the
changes are saved to the image, but when I use a JPG image the changes are not
saved to the image. Is there something missing in the code, do I need to save
the changes , and how would I do ??


Answer: Try with:

    
    
    im.putpixel((50,50), (70, 70, 70))
    

or you can also do:

    
    
    import numpy as np
    from PIL import Image
    
    # pix has 4 channels for png and 3 for jpg
    pix = np.array(im)
    
    pix[50, 50, 0] = 70      # 0 accesses the first channel
    pix[50, 50, 1] = 70      # 1 accesses the second channel
    pix[50, 50, 2] = 70      # 2 accesses the third channel
    
    Image.fromarray(pix).save('new_img.jpg')
    

