"Specular Reflection" consists of 4 operations.
- clone from original image and rotate it
- add transparency to cloned image
- combine original image and cloned one
- resize combined image
- Create canvas with extending the vertical length
- Put the rotated image onto the bottom of this canvas
- Put the original image onto the top of this canvas
Sample of concrete implementation is below:
require 'mini_magick'
original_file = "original.jpg"
image_cloned = MiniMagick::Image.open(original_file)
background_color = 'black' # set background color
COMPOSITE_GRAVITY = 'north'
TRANSPARENCY = '65%'
REFLECTION_FACTOR = Math.sqrt(2)
image_cloned.combine_options do |mogrify|
mogrify.flip
mogrify.fill background_color
mogrify.colorize TRANSPARENCY
end
image_original = MiniMagick::Image.open(original_file)
image_cloned.combine_options(:convert) do |convert|
new_width = image_original[:width]
new_height = (image_original[:height]*REFLECTION_FACTOR).to_i
convert.size "#{new_width}x#{new_height}"
convert.xc background_color
convert.swap '0,1'
convert.gravity COMPOSITE_GRAVITY
convert.geometry "+0+#{image_original[:height]}"
convert.composite
end
image_combined = image_cloned.composite(image_original) do |c|
c.gravity COMPOSITE_GRAVITY
end
output_file = "reflected_#{background_color}.jpg"
image_combined.write output_file
puts "#{output_file} is generated."
These are the output samples.
for white background:
for black background:
No comments:
Post a Comment