Skip to content

Instantly share code, notes, and snippets.

@unkmas
Created February 28, 2021 13:46
Show Gist options
  • Save unkmas/0a5c2923d4e944bcc3c7932fe3b7a69a to your computer and use it in GitHub Desktop.
Save unkmas/0a5c2923d4e944bcc3c7932fe3b7a69a to your computer and use it in GitHub Desktop.
Quick watermark for photos

Quick watermark

run `ruby watermark.rb source_file.png output_file.png "My watermark text"

#!/usr/bin/ruby
require "vips"
im = Vips::Image.new_from_file ARGV[0], access: :sequential
# make the text mask
text = Vips::Image.text ARGV[2], width: 5000, dpi: 150, font: "sans bold 48"
text = text.rotate(-45)
# make the text transparent
text = (text * 0.3).cast(:uchar)
text = text.gravity :centre, 1500, 1500
text = text.replicate 1 + im.width / text.width, 1 + im.height / text.height
text = text.crop 0, 0, im.width, im.height
# we make a constant colour image and attach the text mask as the alpha
overlay = (text.new_from_image [242, 185, 195]).copy interpretation: :srgb
overlay = overlay.bandjoin text
# overlay the text
im = im.composite overlay, :over
im.write_to_file ARGV[1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment