Last active
August 29, 2015 14:08
-
-
Save cxymrzero/2381f66691be7876ea01 to your computer and use it in GitHub Desktop.
将图片的四个角变成透明的脚本
This file contains hidden or 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
# coding: utf-8 | |
# cxy @ 2014.10.28 | |
# 将此脚本与mask.png放在同一目录下,子目录'pic_origin'存放待剪裁的图片 | |
import os | |
from PIL import Image | |
def get_pic_list(dir_str): | |
lst = [] | |
for i in os.listdir(dir_str): | |
# if i.split('.')[-1] == 'png': | |
lst.append(i) | |
return lst | |
def gen_pic(pic_list): | |
if not os.path.exists('pic_gen'): | |
os.mkdir('pic_gen') | |
# 打开mask图片,将模式转换为'L' | |
mask = Image.open('mask.png') | |
mask = mask.convert('L') | |
os.chdir('pic_origin') | |
for pic in pic_list: | |
# pic是图片名 | |
im = Image.open(pic) | |
# putalpha会将mask作为alpha层放入im,可以通过 | |
# im.split()[3].show()查看im的alpha层 | |
im.putalpha(mask) | |
# im.save()后才能看到图片发生了变化,im.show()是看不到的 | |
im.save('../pic_gen/' + pic) | |
def main(): | |
pic_list = get_pic_list('pic_origin') | |
gen_pic(pic_list) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment