Skip to content

Instantly share code, notes, and snippets.

@maxim
Last active December 16, 2015 07:59
Show Gist options
  • Select an option

  • Save maxim/5402399 to your computer and use it in GitHub Desktop.

Select an option

Save maxim/5402399 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# Usage:
# Example command ./coverflowify.rb source.png 30 20 50 true output.png
#
# The values are:
# * shrink percent - specifies by how much percent to shrink the background images
# * skew percent - specifies how much 3d rotation (inward) to give background images
# * visible percent - specifies how much of background images stick out on left and right
# * add_shadow - if true, adds default shadow, also accepts full shadow spec, like "70x4+5+5"
# * output path - just uses "result.png" if omitted
require 'skeptick'
include Skeptick
original, shrink_percent, skew_percent, visible_percent, add_shadow, result = ARGV
raise 'Must supply filename' unless original
shrink_percent ||= 20
skew_percent ||= 30
visible_percent ||= 40
add_shadow = add_shadow == 'true' ? '30x3+3+3' : add_shadow
result ||= 'result.png'
shrink_percent = shrink_percent.to_i
skew_percent = skew_percent.to_i
visible_percent = visible_percent.to_i
def dimensions(path)
`convert -auto-orient #{path} -format "%[fx:w],%[fx:h]" info:`.
strip.split(',').map(&:to_i)
end
w, h = dimensions(original)
ishp = 100 - shrink_percent
iskp = 100 - skew_percent
rw = (w * (ishp / 100.0)).to_i
rh = (h * (ishp / 100.0)).to_i
pdiff = ((rh - (rh * (iskp / 100.0))) / 2.0).to_i
extp = (rw * (visible_percent / 100.0)).to_i
result_width = w + extp
left_img = convert do
clone(0)
apply :matte
apply '-virtual-pixel', 'transparent'
apply :distort
apply 'Perspective',
"'0,0 0,0 0,#{rh} 0,#{rh} #{rw},0 #{rw},#{pdiff} #{rw},#{rh} #{rw},#{rh - pdiff}'"
set :background, 'transparent'
set :gravity, 'west'
set :extent, "#{result_width}x#{h}"
end
right_img = convert do
clone(0)
apply :matte
apply '-virtual-pixel', 'transparent'
apply :distort
apply 'Perspective',
"'0,0 0,#{pdiff} 0,#{rh} 0,#{rh - pdiff} #{rw},0 #{rw},0 #{rw},#{rh} #{rw},#{rh}'"
set :background, 'transparent'
set :gravity, 'east'
set :extent, "#{result_width}x#{h}"
end
background_layer = compose(:dst_over) do
clone(0)
set :resize, geometry(width: rw, height: rh, exact: true)
image left_img
image right_img
delete(0)
end
source_img = if add_shadow
compose(:over, original) do
convert do
clone
set :background, 'black'
set :shadow, add_shadow
end
swap
end
else
original
end
result = compose(:dst_over, to: result) do
set '-auto-orient'
image source_img
image background_layer
set '-auto-orient'
set :gravity, 'center'
set :extent, "#{result_width}x#{h}"
end
result.build
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment