Created
May 25, 2021 17:45
-
-
Save David256/1d40fdf846f52fb8508079327b9cd284 to your computer and use it in GitHub Desktop.
Script to create a GIF with colors
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
#!/usr/bin/python3 | |
# -*- coding: utf-8 -*- | |
from numpy import pi, array, full, uint8 | |
import imageio | |
import matplotlib.pyplot as plt | |
theta = 0.5 # valor entre [0,1) | |
light = 1 # valor entre [0,1) | |
def positive(number): | |
if number < 0: | |
return 0. | |
return number | |
def convert_turning(gamma): | |
print = lambda *x:x | |
assert(gamma <= 1.) | |
# definimos los límites del círculo | |
c0 = 0. | |
c1 = 1/3. | |
c2 = 2/3. | |
c3 = 1. | |
print("gamma entrante:", gamma) | |
# sacamos ahora los valores para los colores | |
red = gamma if (gamma > c0 and gamma < c2) else 0 | |
green = gamma if (gamma > c1 and gamma < c3) else 0 | |
blue = gamma if (gamma > c2 and gamma < c3) else (gamma + c1) if (gamma < c1) else 0 | |
print("valores resultantes:", [red, green, blue]) | |
# normalizamos los valores | |
red = red - c0 | |
green = green - c1 | |
blue = blue if blue < c2 else blue - c2 | |
print("normalización:", [red, green, blue]) | |
# sólo valores positivos | |
red = positive(red) | |
green = positive(green) | |
blue = positive(blue) | |
print("valores positivos:", [red, green, blue]) | |
# expandimos el rango | |
red = red / c2 | |
green = green / c2 | |
blue = blue / c2 | |
print("valores rango normal:", [red, green, blue]) | |
# expandimos más aún | |
red = int(red * 510) | |
green = int(green * 510) | |
blue = int(blue * 510) | |
# cambiamos el sentido | |
red = red if red < 255 else 255 - (red - 255) | |
green = green if green < 255 else 255 - (green - 255) | |
blue = blue if blue < 255 else 255 - (blue - 255) | |
return red, green, blue | |
def show_one(name, value, max): | |
print(name + ": ", end="") | |
bars = int(20*value/max) | |
dots = 20 - bars | |
print("[", end="") | |
print("|"*bars, end="") | |
print("."*dots, end="") | |
print("]", end="") | |
print(" ", end="") | |
def show_bar(rgb): | |
r,g,b = rgb | |
show_one("rojo", r, 255) | |
show_one("verde", g, 255) | |
show_one("azul", b, 255) | |
print("\r", end="") | |
def _main(): | |
ry = [] | |
gy = [] | |
by = [] | |
c0 = 0. | |
c1 = 1/3. | |
c2 = 2/3. | |
c3 = 1. | |
for i in range(100): | |
gamma = i/100 | |
red = gamma if (gamma > c0 and gamma < c2) else 0 | |
green = gamma if (gamma > c1 and gamma < c3) else 0 | |
blue = gamma if (gamma > c2 and gamma < c3) else (gamma + c1) if (gamma < c1) else 0 | |
# normalizamos | |
red = red - c0 | |
green = green - c1 | |
blue = blue if blue < c2 else blue - c2 | |
# eliminamos negativos | |
red = positive(red) | |
green = positive(green) | |
blue = positive(blue) | |
# normal a uno | |
red = red / c2 | |
green = green / c2 | |
blue = blue / c2 | |
# hacemos algo más | |
red = int(red * 510) | |
green = int(green * 510) | |
blue = int(blue * 510) | |
red = red if red < 255 else (255 - (red - 255)) | |
green = green if green < 255 else (255 - (green - 255)) | |
blue = blue if blue < 255 else (255 - (blue - 255)) | |
# agregamos | |
ry.append(red) | |
gy.append(green) | |
by.append(blue) | |
plt.plot(ry, "r") | |
plt.plot(gy, "g") | |
plt.plot(by, "b") | |
plt.show() | |
def main(): | |
fps = 20 # (?) | |
seconds = 30 | |
frames = seconds*fps | |
imgs = [] | |
for i in range(frames): | |
#print(i/frames, "porcentaje") | |
gamma = i/frames | |
rgb = convert_turning(gamma) | |
im = full((300,300,3), rgb, dtype=uint8) | |
imgs.append(im) | |
show_bar(rgb) | |
print() | |
print("creando gif...") | |
imageio.mimsave('colores.gif', imgs, duration=1/seconds, subrectangles=True) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment