Skip to content

Instantly share code, notes, and snippets.

@asnramos
Created July 23, 2020 04:34
Show Gist options
  • Save asnramos/07b0f83da9b580c0e82f17cc448182c0 to your computer and use it in GitHub Desktop.
Save asnramos/07b0f83da9b580c0e82f17cc448182c0 to your computer and use it in GitHub Desktop.
Sine wave foto effects animation
from PIL import Image
import numpy as np
import matplotlib.pyplot as pyplot
import matplotlib.animation as animation
fle = "Jup" # File name
img = Image.open("C:\\Users\\Rober\\Desktop\\"+fle+".png") # Works only with png files - adjust to your directory
# Number of horizontal and vertical lines
h = img.size[0]
v = img.size[1]
R,G,B = img.split() # Splitting the image in Red, Blue and Green channel
s = 1 # Resolution : 1 = full resolution
# Converting the channels into numpy arrays
rsizeArr_R = np.asarray( R.resize((int(h/s),int(v/s))) )
rsizeArr_G = np.asarray( G.resize((int(h/s),int(v/s))) )
rsizeArr_B = np.asarray( B.resize((int(h/s),int(v/s))) )
# Minor plot adjustments
fig, g = pyplot.subplots(figsize=(h/100,v/100))
g.axis("off")
pyplot.subplots_adjust(top = 1.00, left = 0.00, right = 1.00, bottom = -0.00)
def mosaic(va) :
def rotH_1(array,va) : # Displacement function
# va - slidding variable
amp = 0.50 # Amplitude, tested 0.5 (smaller) and 0.25 (larger) with good visual effects,
# you may need to change the number of frames accordingly.
ncols = len(array[0])
zc = np.arange(0, ncols*5, amp)[va:va+ncols] # A very big array to slide
ns1 = list( np.cos(zc) * 3 ) # The "equation", 3 is a multiplier you can change your taste
c = 0
for n in range(0,ncols) :
ns = int(ns1[n])
t = array[:,n]
if ns >=0 :
for a in range(0,ns) :
t = np.append(t,t[0])
t = np.delete(t,0)
else :
for b in range(ns,0) :
t = np.append(t[-1],t)
t = np.delete(t,-1) # 0
if c == 0 :
z = t
else :
z = np.vstack( [z,t] )
c +=1
return z.transpose()
Arr_B = rotH_1(rsizeArr_B, va) # Calling the function - in this case I only used the blue channel,
# but you can use more and add imshow accordingly.
pyplot.imshow(Arr_B, cmap = "bone" ,alpha = 1, interpolation= "none")
pyplot.grid(False)
pyplot.axis("OFF")
return
fr = 13 # Number of frames : tested 13 for amp = 0.5 and 26 for amp = 0.25.
ctd = 0 # Counter
def update(*args) : # Animation function
global ctd
pyplot.clf()
mosaic(ctd)
ctd +=1
return()
def init() : # # More animation function stuff
return()
anim = animation.FuncAnimation(fig, update, init_func=init, blit = True, frames=fr, repeat = False)
sf = fle+".gif"
Sname = "C:\\Users\\rober\\Desktop\\_" + sf
anim.save(Sname, writer="imagemagick", fps=30) # fps = Frames Per Second
# You must have imagemagick installed in you computer :
# https://www.imagemagick.org/script/index.php
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment