I knew I had this somewhere...
Here's a function I sometimes use to convert all the white pixels to transparent.
It uses img.load()
to get a modifiable sequence of the image's pixels, which are then modified.
- Code: Select all
def make_transparent(original_name, new_name):
img = Image.open(original_name).convert('RGBA')
pixels = img.load()
width, height = img.size
for y in xrange(height):
for x in xrange(width):
if pixels[x, y] == (255, 255, 255, 255):
pixels[x, y] = (255, 255, 255, 0)
It's pretty easy to modify to do what you need.
Although it's still possible there's a better solution.