Skip to content

Instantly share code, notes, and snippets.

@huiliu
Created November 13, 2018 09:34
Show Gist options
  • Save huiliu/0c5d8b3c4966a3a8de27788c65aa8cfe to your computer and use it in GitHub Desktop.
Save huiliu/0c5d8b3c4966a3a8de27788c65aa8cfe to your computer and use it in GitHub Desktop.
将png图片中的指定颜色设置为透明
# 将图片中的指定颜色设置为透明
# 运行于python3.7 Pillow5.3
import sys
from PIL import Image
def Convert(fileName):
image = Image.open(fileName)
img = image.convert("RGBA")
for r in range(img.height):
for c in range(img.width):
p = img.getpixel((r, c))
# 将白色透明化
if p[0] == 255 and p[1] == 255 and p[2] == 255:
p = (p[0], p[1], p[2], 0)
img.putpixel((r, c), p)
img.save(fileName)
if __name__ == '__main__':
if len(sys.argv) >= 2:
Convert(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment