nukl
nukl

Reputation: 10531

Converting grayscale png with transparency using PIL

PIL corrupt png images with transparency if i make them grayscale. Why?

Here's my code:

input = Image.open('input.png')
output = ImageOps.grayscale(input)
output.save('output.png', **input.info)

Input

http://imgur.com/a/m50p6

Output

http://imgur.com/a/m50p6

is there a way to fix that?

Upvotes: 5

Views: 4930

Answers (2)

Seril
Seril

Reputation: 11

I ran into this issue as well. The only solution I've been able to find is to convert to 'LA' and then back to 'RGBA'

Try:

Image.open('input.png').convert('LA').convert('RGBA')

I was attempting to display the resulting grayscale PNG with transparency on a tkinter canvas, but I think this method will probably also work for saving the output.

Upvotes: 1

tito
tito

Reputation: 13271

You can use convert method with luminance trick:

Image.open('input.png').convert('LA').save('output.png')

Upvotes: 10

Related Questions