Skip to content

Instantly share code, notes, and snippets.

@eaorak
Created July 25, 2016 11:32
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 eaorak/17e475523d37e3fa865cd31e26239d8f to your computer and use it in GitHub Desktop.
Save eaorak/17e475523d37e3fa865cd31e26239d8f to your computer and use it in GitHub Desktop.
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
os.chdir('image_dir')
for file in os.listdir('.'):
if not file.endswith('.gif'):
continue
im = Image.open(file)
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)
im.save(file)
@eaorak
Copy link
Author

eaorak commented Jul 25, 2016

Original reference:
http://stackoverflow.com/questions/6483489/change-the-color-of-all-pixels-with-another-color

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