0

Using the following code, PIL easily returns an array of single pixel values from an image. Not sure what the term for it is; but instead of a 3d array (RGB), it simplifies each pixel into one of 256 values.

from PIL import Image
im = Image.open(image_path, 'r')
pixel_values = list(im.getdata())

The question is, how can I edit pixels on an image with this same method? I believe the default arg for the putpixel method expects a 3d array (RGB), and if I only give one value; it only ranges over shades of black.

im.putpixel((x, y), value)
im.show()

I would like to be able to substitute integers (0-255) in for value and have access to the wider spectrum of discrete colors.

Is this possible? Seems like it should already be a built in method.

  • It's variously called a *"palette image"*, or *"indexed image"* https://stackoverflow.com/a/52307690/2836621 – Mark Setchell Jun 06 '22 at 22:18
  • @MarkSetchell Thanks for noting the terminology. Though I don't see info on that thread for updating pixels using palette mode, only reading them. – intuiting_math Jun 06 '22 at 22:25
  • As with your other question, you didn't make it easy for folks to help you. So we can't see your image, or its type, or how many palette entries it has. – Mark Setchell Jun 06 '22 at 22:29

0 Answers0