简体   繁体   中英

Pink tint on pixel conversion using PIL Image

I am trying to change half of the pixels in an image and there is always a pink tint to them. Even in an example of the code where the pixel RGB value remains the same, the image comes out tinted pink. Here is the code and the before and after image. What is causing the pink tint and how can I fix it?

from PIL import Image
im = Image.open('me.jpg')
pix = im.load()
q, w = (im.size)
s=0
a=0
x=0
y=0
while s<w:
    while a<(q/2):
        r, g, b = im.getpixel((x,y))
        pix[0+x,0+y] = (r,b,g)
        a=a+1
        x=x+1
    a=0
    x=0
    y=y+1
    s=s+1

im.save('me2.jpg')

Example

You've reversed the order of g and b when assigning the colours you extracted:

pix[0+x,0+y] = (r,b,g)

If you put them back in rgb order, it works:

pix[0+x,0+y] = (r,g,b)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM