Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 497
nukl Avatar asked Jan 19 '23 01:01

nukl


2 Answers

You can use convert method with luminance trick:

Image.open('input.png').convert('LA').save('output.png')
like image 185
tito Avatar answered Jan 28 '23 15:01

tito


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.

like image 23
Seril Avatar answered Jan 28 '23 16:01

Seril