Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert an partially alpha transparent image to have single (non-transparent) colour in PIL?

I have a logo which I need to make into a monochrome version, for example all red. For example, convert this:

enter image description here

to this:

enter image description here

I've tried various techniques in PIL (mainly Image.paste), but the results do not honour the partially alpha transparent pixels around the edges.

like image 978
Euan Avatar asked Oct 20 '22 15:10

Euan


1 Answers

from PIL import Image

image = Image.open('logo.png')
assert im.mode.endswith('A'), 'This will only work with images having alpha!'

image.load()
alpha = image.split()[-1]
image2 = Image.new(image.mode, image.size, (255, 0, 0, 0))
image2.putalpha(alpha)

image2.save('logo-red.png')
like image 133
Mark Ransom Avatar answered Oct 23 '22 11:10

Mark Ransom