# Script to take input images and add an alpha channel based on colour values. Assumes the inputs are jpgs and
# outputs PNGS, so may overwrite PNGs - be careful !

import PIL
from PIL import Image
import os
import bpy

# Change to the directory where the .blend file is
blenddir = bpy.path.abspath("//")
os.chdir(blenddir)

files = os.listdir()

n = 0

for filename in files:
    if ('.png' in filename) or ('.PNG' in filename):
        n = n + 1
        print(n,'/',len(files),filename)
        img = Image.open(filename)
        
        img_withalpha = img.copy()
        
        imalpha = img.convert('L')
        
        img_withalpha.putalpha(imalpha)
        
        outname = filename.split('.')[0]+'.png'
        
        img_withalpha.save(outname)
        