Created
November 13, 2018 09:34
-
-
Save huiliu/0c5d8b3c4966a3a8de27788c65aa8cfe to your computer and use it in GitHub Desktop.
将png图片中的指定颜色设置为透明
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 将图片中的指定颜色设置为透明 | |
# 运行于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