forked from ivansipiran/grafica
-
Notifications
You must be signed in to change notification settings - Fork 5
/
ex_color_palette_anim.py
58 lines (42 loc) · 1.59 KB
/
ex_color_palette_anim.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# coding=utf-8
"""Animation changing the color palette while simulating an indirect color scheme with matplotlib"""
from ex_color_palette import *
import sys, os.path
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from grafica.assets_path import getAssetPath
__author__ = "Daniel Calderon"
__license__ = "MIT"
def updatePalette(colorPalette, t):
newPalette = []
for color in colorPalette:
# Generating a new color changing the RGB order...
newColor = np.array([
color[0],
color[1] * t,
color[2] * t],
dtype=np.float)
newPalette += [newColor]
return newPalette
if __name__ == "__main__":
# Reading an image into a numpy array
originalImage = mpl.imread(getAssetPath("santiago.png"))
# Removing alpha channel if present
if originalImage.shape[2] == 4:
originalImage = originalImage[:,:,0:3]
# Obtaining all different colors in the image and the indexed image
indexedImage, colorPalette = getColorPalette(originalImage)
# Reconstructing image
animatedImage = assignColors(indexedImage, colorPalette)
fig, ax = mpl.subplots()
im = ax.imshow(originalImage, animated=True)
time = 0
def updateFig(*args):
global time
time += 0.1
param = np.abs(np.sin(time))
newColorPalette = updatePalette(colorPalette, param)
updatedImage = assignColors(indexedImage, newColorPalette)
im.set_array(updatedImage)
return im,
ani = animation.FuncAnimation(fig, updateFig, interval=50, blit=True)
mpl.show()