user3773503
user3773503

Reputation: 161

Python PIL library - Saving an image from a RGBA array

I am currently using the PIL library to manipulate the pixel colours in an image.

My problem is that I don't know how to save a rgba array into a png file.

image = Image.open(filepath)
pixels = image.load()

...after changing the rgba values in pixels e.g.

pixels[2,10] = 30, 40, 50, 60

Then if I try to save pixels like this:

pixels.save(path)

i get error

AttributeError: 'PixelAccess' object has no attribute 'save'

How do I then save pixels back into an png file?

(sorry in advance if this is a stupid question)

Thanks! Andrew

Upvotes: 4

Views: 12756

Answers (1)

Kevin
Kevin

Reputation: 76184

It's the Image object that has the save method, not the pixel access object.

image.save(path)

Modifying the pixel access object automatically causes the image to be modified too. So saving the image is effectively the same as saving the pixel access object.

Upvotes: 8

Related Questions