Skip to content

Instantly share code, notes, and snippets.

@fcaylus
Created October 30, 2016 13:03
Show Gist options
  • Save fcaylus/cf9bae44b7ce418668c53e7619b93ba8 to your computer and use it in GitHub Desktop.
Save fcaylus/cf9bae44b7ce418668c53e7619b93ba8 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
#
# Change a specified color to an another one for all *.png files in current dir
#
import glob
import numpy as np
from PIL import Image
# Original values
r1, g1, b1 = 0, 65, 220
# Value that we want to replace it with
r2, g2, b2 = 0, 0, 0
for filename in glob.glob("./*.png"):
im = Image.open(filename)
data = np.array(im)
red, green, blue = data[:,:,0], data[:,:,1], data[:,:,2]
mask = (red == r1) & (green == g1) & (blue == b1)
data[:,:,:3][mask] = [r2, g2, b2]
im = Image.fromarray(data)
im.save(filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment