Skip to content

Instantly share code, notes, and snippets.

@offlinehacker
Created January 20, 2013 23:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save offlinehacker/4582605 to your computer and use it in GitHub Desktop.
Save offlinehacker/4582605 to your computer and use it in GitHub Desktop.
import os
import sys
import Image, ImageSequence
import numpy as np
from images2gif import writeGif
filename = sys.argv[1]
im = Image.open(filename)
original_duration = im.info['duration']
frames = [np.array(frame.copy()) for frame in ImageSequence.Iterator(im)]
shape = frames[0].shape
print "Sequence size is", len(frames), "with duration", original_duration
print "Image size is:", frames[0].shape
var = .05
gain = 0.8
size = len(frames[0].flatten())
measure_var = np.ones(size) * var
predict = frames[0].flatten()/255.
predict_var = np.copy(measure_var)
correct = np.zeros(size)
correct_var = np.zeros(size)
K = np.zeros(size)
frame50pre = None
frame50post = None
for no in range(1,len(frames)):
print "next frame"
if(no==50):
frame50pre = np.copy(frames[no])
flat = frames[no].flatten()/255.
K = predict_var / (predict_var + measure_var)
flat = gain * predict + (1.0 - gain) * flat + K * (flat - predict)
correct_var = predict_var * (1.0 - K)
predict_var = correct_var
predict = flat
frames[no] = flat.reshape(shape)
if(no==50):
frame50post = np.copy(frames[no]*255)
writeGif("processed_" + os.path.basename(filename), frames, duration=original_duration/1000.0, dither=0)
x1,y1 = Image.fromarray(frame50pre).size
x2,y2 = Image.fromarray(frame50post).size
im = Image.new("L", (x1+x2,y1))
im.paste(Image.fromarray(frame50pre), (0,0))
im.paste(Image.fromarray(frame50post), (x1,0))
im.save("diff_" + os.path.basename(filename))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment