# -*- coding: utf-8 -*- import os import Image IN_DIR = "C:/Users/Karolis/Dropbox/rom/static/images/players" # Absolute path of directory with images. OUT_DIR = "transparent_images" # Name of directory with output contents. PIXELS_TO_CONVERT = (255, 255, 255) # Pixels to be rewritten to transparent colour. os.chdir(IN_DIR) # Moving to input directory. try: os.mkdir(OUT_DIR) # Creating output directory. except WindowsError: pass for f in os.listdir(os.getcwd()): if not ".." in f: img = Image.open(f) # Opening image. img = img.convert("RGBA") # Converting to RGBA format. pixels = img.load() # Pixel map. # Looping through pixels and making them transparent where relevant. for y in xrange(img.size[1]): for x in xrange(img.size[0]): if pixels[x, y] == PIXELS_TO_CONVERT: pixels[x, y] = (255, 255, 255, 0) os.chdir(OUT_DIR) # Moving to output directory. img.save(f, "PNG") # Saving transparent image. os.chdir(IN_DIR) # Returning back to input directory.