Skip to content

Instantly share code, notes, and snippets.

@cameronbarker
Last active August 29, 2015 14:07
Show Gist options
  • Save cameronbarker/8ee5b6134493a1f8ed39 to your computer and use it in GitHub Desktop.
Save cameronbarker/8ee5b6134493a1f8ed39 to your computer and use it in GitHub Desktop.
SPLT Gif Maker
=begin
objective::
Take two similar images, overlay them and show the difference between them.
example output: https://github.com/cameronbarker/cameronbarker.github.io/blob/master/prototype/splt/img/art-project.gif
requirements::
ruby 2.1.1
image size 616x616
running::
splt = SpltGif.new
crop
cropped_paths = splt.crop("foreground.jpg")
combine cropped images with background image
combined_paths = splt.combine(cropped_paths, "background.jpg")
animate the images
animated_gif = splt.animate(combined_paths)
=end
require 'open-uri'
require 'RMagick'
class SpltGif
def crop(img)
size = [1, 77, 154, 231, 308, 385, 462, 539, 616]
paths = Array.new
# get name
parsed_imgage_name = URI.parse(img).path[1..-5]
size.each do |s|
image = Magick::ImageList.new
urlimage = open(img)
image = image.from_blob(urlimage.read)
# crop image
image = image.crop(Magick::NorthEastGravity, s, 616, true)
# create name
image_name = parsed_imgage_name + "-" + s.to_s + ".jpg"
# temp save it
image.write(image_name)
# push name to array for return
paths << image_name
# debug
puts image_name
end
return paths
end
def combine(cropped_paths, background_img)
paths = Array.new
cropped_paths.each do |p|
#create combined
image = Magick::ImageList.new(p)
#init background
background = Magick::ImageList.new
#pull background
urlimage = open(background_img)
#create background
background = background.from_blob(urlimage.read)
#set gravity
background.gravity = Magick::EastGravity
#combine photos
combined = background.composite_layers(image, Magick::OverCompositeOp)
combined.format = "JPG"
updated_name = p[0..-5] + ".jpg"
combined.write(updated_name)
paths << updated_name
puts updated_name
end
return paths
end
def animate(paths)
animation = Magick::ImageList.new()
#update the array order
updated_array = [paths[4], paths[5], paths[6], paths[7], paths[8], paths[7], paths[6], paths[5], paths[4], paths[3], paths[2], paths[1], paths[0], paths[1], paths[2], paths[3]]
# loop through image paths to add to list
updated_array.each do |i|
animation.read i
puts i
end
# animation delay 1 second == 20
animation.delay = 20
final_name = paths.first[0..-8] + "-GIF.gif"
animation.format = "GIF"
animation.write(final_name)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment