Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method for converting PNGs to premultiplied alpha [closed]

Looking for some kind of simple tool or process for Windows that will let me convert one or more standard PNGs to premultiplied alpha.

Command line tools are ideal; I have easy access to PIL (Python Imaging Library) and Imagemagick, but will install another tool if it makes life easier.

Thanks!

like image 718
Ipsquiggle Avatar asked Jul 06 '11 03:07

Ipsquiggle


Video Answer


1 Answers

A more complete version of the cssndrx answer, using slicing in numpy to improve speed:

import Image
import numpy

im = Image.open('myimage.png').convert('RGBA')
a = numpy.fromstring(im.tostring(), dtype=numpy.uint8)
alphaLayer = a[3::4] / 255.0
a[::4]  *= alphaLayer
a[1::4] *= alphaLayer
a[2::4] *= alphaLayer

im = Image.fromstring("RGBA", im.size, a.tostring())

Et voila !

like image 102
François Lagunas Avatar answered Oct 02 '22 20:10

François Lagunas