Skip to content

Commit

Permalink
Major rewrite: timelapse.jl to Julia v1.0.0 and Images.jl
Browse files Browse the repository at this point in the history
Significant language changes:
no more find() to return index in array - tidier reimplementation saves an exception
no glob()
requires Printf library to use @sprintf

Use load()/save() not imread()/imwrite()
  • Loading branch information
spodzone committed Sep 4, 2018
1 parent 0079337 commit 184483e
Showing 1 changed file with 27 additions and 22 deletions.
49 changes: 27 additions & 22 deletions timelapse.jl
@@ -1,25 +1,25 @@
#!/usr/bin/env julia

@info("Starting")
function tlog(str)
tm=time()
println("[$tm] - $str")
end

tlog("Starting")

@info("Loading modules")
tlog("Loading modules")

#using Graphics, Images, Color, FixedPointNumbers
using Graphics, Images, FixedPointNumbers
using Images, Printf

function imageinterpolate(img1, img2, prop=0.5)
"Return a new image interpolated prop proportion of the way from img1 to img2"
img1b=reshape(data(img1),(width(img1)*height(img1)))
img2b=reshape(data(img2),(width(img2)*height(img2)))

d=[ img1b[i]+ (img2b[i]-img1b[i])*prop for i in 1:length(img1b) ]
img3b=reshape(d, (width(img1), height(img1)))
copy(img1, img3b)
d = img1 + (img2-img1)*prop
d
end

function findimages(data, t)
"Find the indexes and proportion between them corresponding to time t in data"
idx=try minimum(find(map(x->x[1]>t, data)))-1 catch "" length(data) end
idx=length(filter( x -> x[1]<t, data))
left=max(1, idx)
right=min(idx+1, length(data))
tl=data[left][1]
Expand All @@ -32,18 +32,18 @@ function findimages(data, t)
end


@info("Sorting parameters")
noframes=try int(ARGS[1]) catch "" 100 end
imagedir=try ARGS[2] catch "" "./images" end
outdir=try ARGS[3] catch "" "./images-out" end
tlog("Sorting parameters")
noframes=try parse(Int, ARGS[1]) catch N 100 end
imagedir=try ARGS[2] catch N "./images" end
outdir=try ARGS[3] catch N "./images-out" end

@info("Reading images from directory [$imagedir]")
tlog("Reading images from directory [$imagedir]")
images=sort(readdir(imagedir))
images=map(x->"$imagedir/$x", images)
imagetimes = map(x->stat(x).mtime, images)
filedata=sort([ (imagetimes[i], images[i]) for i in 1:length(images) ])

@info("Interpolating $noframes frames")
tlog("Interpolating $noframes frames")

tstart=minimum(imagetimes)
tend=maximum(imagetimes)
Expand All @@ -53,12 +53,17 @@ oldleft=-1
for i in 1:noframes
desiredtime=tstart+(tend-tstart)*i/noframes
left,right,prop=findimages(filedata, desiredtime)
info(" frame $i / $noframes left=$left, right=$right, prop=$prop")
img1=imread(images[left])
img2=imread(images[right])
tlog(" frame $i / $noframes left=$left, right=$right, prop=$prop")
img1=load(images[left])
img2=load(images[right])
img1 = RGB{Float16}.(img1)
img2 = RGB{Float16}.(img2)

oimg=imageinterpolate(img1, img2, prop)
oimg=RGB{N0f8}.(oimg)
ofile=@sprintf("%s/image-%05d.jpg", outdir, i)
imwrite(oimg, ofile)
tlog(" saving $ofile")
save(ofile, oimg)
end

@info("All done")
tlog("All done")

0 comments on commit 184483e

Please sign in to comment.