Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Change a certain color (RGB) in a GIF file to something else for all files in the directory.
__author__ = 'ender'
import numpy as np
import Image
import os
for file in os.listdir('.'):
if not file.endswith('.gif'):
im =
im = im.convert('RGB')
data = np.array(im)
r1, g1, b1 = 192, 0, 0 # Original value
r2, g2, b2 = 30, 138, 203 # Value that we want to replace it with
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)
Copy link

eaorak commented Jul 25, 2016

Original reference:

Changes made for all the files and gif conversion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment