Skip to content

Instantly share code, notes, and snippets.

@Zuckonit
Created February 17, 2014 03:18
Show Gist options
  • Save Zuckonit/9044135 to your computer and use it in GitHub Desktop.
Save Zuckonit/9044135 to your computer and use it in GitHub Desktop.
替换图片白色为透明
#!/usr/bin/env python
#-*- coding:utf-8 -*-
from PIL import Image
import sys
def color2alpha(img, *color):
image = Image.open(img).convert("RGBA")
datas = image.getdata()
newData = []
for item in datas:
#print item[0], color[0]
if item[0] == color[0] and item[1] == color[1] and item[2] == color[2]:
newData.append((color[0], color[1], color[2], 0))
else:
newData.append(item)
image.putdata(newData)
image.save("image.png", "PNG")
def white2alpha(img):
color2alpha(img, 255,255,255)
if __name__ == '__main__':
white2alpha(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment