Skip to content

Instantly share code, notes, and snippets.

@unimatrixZxero
Created August 15, 2009 00:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unimatrixZxero/168172 to your computer and use it in GitHub Desktop.
Save unimatrixZxero/168172 to your computer and use it in GitHub Desktop.
Script to watermark and resize images with imageMagick
#!/opt/local/bin/ruby
# _ __ _ _____
# __ ______ (_)___ ___ ____ _/ /______(_) _/__ / _ _____ _________
# / / / / __ \/ / __ \__ \/ __ \/ __/ ___/ / |/_/ / / | |/_/ _ \/ ___/ __ \
# / /_/ / / / / / / / / / / /_/ / /_/ / / /> < / /___> </ __/ / / /_/ /
# \__,_/_/ /_/_/_/ /_/ /_/\__,_/\__/_/ /_/_/|_| /____/_/|_|\___/_/ \____/
#
# Watermark folder script
# 2009-08-15 01:52 am
#
# Usage:
# - configure the Watermarker::Where constant to where your pictures are
# - run the script
# - there is no step 3
#
# Known issues:
# - with very narrow images e.g. < 200px this doesn't produce nice results since there isn't much space to work with
#
#
# The MIT License#
# Copyright (c) 2009 Sam Figueroa
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
require 'rubygems'
require "RMagick"
include Magick
class Watermarker
Where = '/Users/somebodyies/Desktop/output'
UseBorder = false
Resize = '1280x851>'
DisplayName = "Sam Figueroa - photography"
ReferenceURL = "http://samuelfigueroa.com/"
LicenseUsed = "(creative commons 3.0) Attribution-NonCommercial-NoDerivs License"
LicenseUsedShort = "(cc 3.0) by-nc-nd License"
LicenseUsedPico = "(cc) by-nc-nd"
BorderStrength = 5
Padding = 2
NoticeStrength = 24
def initialize
@picture = nil;
end
def width
@picture.columns
end
def height
@picture.rows
end
def mark(img)
@picture = Magick::Image.read(img).first
if Resize
@picture.change_geometry!(Resize) { |cols, rows, image|
image.resize!(cols, rows)
}
end
puts "Marking: #{img} #{width}x#{height}"
draw_borders
place_composite(create_bottom_bar, 'south')
create_copyright.draw(@picture) if wide_enough?
create_name.draw(@picture)
create_url.draw(@picture) if wide_enough?
out = img.sub(/\./, "-final.")
puts "Writing: #{out}"
@picture.write(out)
end
def wide_enough?
width > 400
end
def license_text
return LicenseUsed if width > 870
case width
when 510..870 then LicenseUsedShort
when 0..509 then LicenseUsedPico
end
end
def image_factory(width, height, color="rgba(0,0,0,1)")
Magick::Image.new(width, height){ self.background_color = color }
end
def text_factory(size=10, color='white', font='Lucida Grande', weight=BolderWeight)
t = Draw.new
t.pointsize(size)
t.fill(color)
t.font_family(font)
t.font_weight(weight)
t.fill_opacity(0.80)
end
def place_composite(composite, where='north')
@picture.composite!(composite, Kernel.const_get("#{where.capitalize!}Gravity"), BumpmapCompositeOp)
# @picture.composite!(composite, Kernel.const_get("#{where.capitalize!}Gravity"), MultiplyCompositeOp)
end
def draw_borders
if(UseBorder)
place_composite image_factory(width, BorderStrength), 'north'
place_composite image_factory(width, BorderStrength), 'south'
place_composite image_factory(BorderStrength, height), 'west'
place_composite image_factory(BorderStrength, height), 'east'
end
end
def text_position_vertical
height - (5 + BorderStrength)
end
def create_bottom_bar
image_factory(width, NoticeStrength, "rgba(0,0,0,0.7)")
end
def create_copyright
copyright = text_factory
copyright.text_align(RightAlign)
copyright.text(text_position_right,
text_position_vertical,
"#{license_text} #{Date.today.year}")
end
def create_name
name = text_factory(12)
h_position = text_position_left
if wide_enough?
name.text_align(CenterAlign)
h_position = width / 2
end
name.text(h_position, text_position_vertical, DisplayName)
end
def create_url
url = text_factory
url.text(BorderStrength + Padding, text_position_vertical, ReferenceURL)
end
def text_position_right
width - BorderStrength - Padding
end
def text_position_left
BorderStrength - Padding
end
end
from_where = ARGV[0] || Watermarker::Where
files = Dir.new(from_where)
watermarker = Watermarker.new
files.each do |f|
watermarker.mark("#{from_where}/#{f}") unless f.match(/-final\./) || f.start_with?('.')
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment