Skip to content

Instantly share code, notes, and snippets.

@dwaynemac
Last active December 23, 2015 08:49
Show Gist options
  • Save dwaynemac/6610761 to your computer and use it in GitHub Desktop.
Save dwaynemac/6610761 to your computer and use it in GitHub Desktop.
Script to add a watermark to a directory of images.
ORIGINAL_DIR= 'fotos'
OUTPUT_DIR = 'overlayed'
OVERLAY_FILE = 'overlay.png'
class Composer
def initialize(file_name)
@file_name = file_name
end
def image_dimensions
@image_dimensions ||= `identify -format '%wx%h' #{ORIGINAL_DIR}/#{@file_name}`
end
def image_height
@h ||= image_dimensions.match(/x(\d+)/)[1]
end
def image_width
@w ||= image_dimensions.match(/(\d+)x/)[1]
end
def overlay_width
@overlay_width ||= image_width.to_i * 0.2
end
def overlay_height
@overlay_height ||= image_height.to_i * 0.2
end
def add_watermark
`composite -size 800x600 -quality 100% -gravity SouthEast -geometry #{overlay_width}x#{overlay_height}+100+100 #{OVERLAY_FILE} #{ORIGINAL_DIR}/#{@file_name} #{OUTPUT_DIR}/#{@file_name}`
end
def resize(dimension = "600x400")
`convert #{OUTPUT_DIR}/#{@file_name} -resize #{dimension} #{OUTPUT_DIR}/#{@file_name}`
end
end
Dir.foreach(ORIGINAL_DIR) do |file|
next if file == '.' or file == '..'
c = Composer.new(file)
c.add_watermark
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment