Created
July 25, 2016 11:32
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.