Skip to content

Instantly share code, notes, and snippets.

@cypai
Created July 23, 2017 07:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cypai/0fc706065baaad4a5f7bbfce68c7b71f to your computer and use it in GitHub Desktop.
Save cypai/0fc706065baaad4a5f7bbfce68c7b71f to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import argparse
from scipy import misc
import numpy as np
def process_image(filepath, outfile):
image = misc.imread(filepath)
if image.shape[2] == 3:
alpha = np.expand_dims(np.full(image.shape[:2], 255), axis=2)
image = np.append(image, alpha, axis=2)
width, height = image.shape[1], image.shape[0]
color_key = image[0,0,:3]
for x in range(width):
for y in range(height):
pixel = image[y,x,:3]
if np.array_equal(pixel, color_key):
image[y,x,3] = 0
misc.imsave(outfile, image)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("source_image")
parser.add_argument("output_image")
args = parser.parse_args()
process_image(args.source_image, args.output_image)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment