Reputation: 4940
I would like to know how to change more than one pixel value in a mode P
image with PIL
.
In my case I have 3 pixels values: 0, 1 , 2. I would map them respectively to 255, 76, 124.
I tried:
Image.open(my_image).point(lambda p: 255 if p==0 else (76 if p==1 else 124))
When I run the code above, I get an image with all black pixels.
Why?
Sholud I use a different function rather than point()
?
Update:
.getpalette()
returns {0, 255}
Upvotes: 0
Views: 476
Reputation: 155
If all pixels in your image with the same value mapping to the same output value sounds fine, then Image.point()
is definitely the right way to go.
Now why you're getting a black image depends on the color values defined in the palette. You can check the image's palette by calling Image.getpalette()
. If your palette defines only 3 color values and anything beyond index 9 in the palette data is 0, then you should map your pixel data in that range, anything other than that will map to the default black.
If you want to use other color values than these defined in your palette, then consider converting to other color modes before calling Image.point()
:
Image.open(my_image).convert('L').point(lambda p: 255 if p==0 else 76 if p==1 else 124)
Upvotes: 1