Skip to content

Instantly share code, notes, and snippets.

@mlabbe
Created June 18, 2015 02:29
Show Gist options
  • Select an option

  • Save mlabbe/e2e89a017dca7c16c150 to your computer and use it in GitHub Desktop.

Select an option

Save mlabbe/e2e89a017dca7c16c150 to your computer and use it in GitHub Desktop.
Premultiply Alpha
from PIL import Image
import numpy
import glob
for path in glob.glob("*.png"):
print(path)
im = Image.open(path).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())
im.save(path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment