Skip to content

Instantly share code, notes, and snippets.

@asnramos
Created July 23, 2020 04:33
Show Gist options
  • Save asnramos/53a57487ff28999f8bbd89b36a56c659 to your computer and use it in GitHub Desktop.
Save asnramos/53a57487ff28999f8bbd89b36a56c659 to your computer and use it in GitHub Desktop.
Sine wave distortion - both axis
from PIL import Image
import numpy as np
import matplotlib.pyplot as pyplot
import matplotlib.animation as animation
fle = "Vermeer" # 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,amp) : # Displacement function (vertical)
# amp = 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 ) # Just a simple equation - no monkey bussuness like artificial intelligence
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_R = rotH_1(rsizeArr_R, va, 0.25) # Calling the function - in this case I only used the red channel,
# but you can use more and add imshow accordingly - horizontal wave
Arr_R = rotH_1(Arr_R.transpose(), va, 0.25).transpose() # Calling again, but now transposed for
# the vertical "wave"
pyplot.imshow(Arr_R, cmap = "gist_heat" ,alpha = 1, interpolation= "none")
pyplot.grid(False)
pyplot.axis("OFF")
return
fr = 26 # 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