Skip to content

Instantly share code, notes, and snippets.

@tgmerritt
Forked from adityashedge/watermark_with_image.rb
Created September 25, 2017 21:03
Show Gist options
  • Save tgmerritt/e76d35e2161ecad12f917b0c0f2d6978 to your computer and use it in GitHub Desktop.
Save tgmerritt/e76d35e2161ecad12f917b0c0f2d6978 to your computer and use it in GitHub Desktop.
Watermark images with ImageMagick 'convert' using 'composite', 'watermark' and 'dissolve' in Ruby
# Watermarking image with another image using Imagemagick 'composite', 'watermark' and 'dissolve'.
require "rubygems"
require "RMagick"
require "pry"
include Magick
# Read the image in the memory with RMagick
puts "What is the image file you want to watermark?"
puts "Drag and drop the file here or enter the full path to the image"
path = gets.chomp
path = "/Users/i851546/Screen Shot 2017-09-14 at 12.45.40.png" if path.length == 0
img = Magick::Image.read(path).first
# the original image was in jpg format
# need to make the white background color transparent
# also changed the format to png since JPG does not support transparency.
# run the command below to create an image with transparent background using ImageMagick
# convert cc.png -transparent white -fuzz 2% watermark.png
puts "What watermark file do you want to use?"
puts "Drag and drop the file here or enter the full path to the image"
watermark_path = gets.chomp
watermark_path = "/Users/i851546/Documents/SAP/sap_anywhere_logo.png" if watermark_path.length == 0
mark = Magick::Image.read(watermark_path).first
# set the canvas to transparent
# if we do not specify 'background_color' on 'mark' then on rotation the background color will be black.
# we want it to be transparent.
mark.background_color = "Transparent"
# resize the watermark to 60% of the image we want to watermark
watermark = mark.resize_to_fit(img.rows * 0.6, img.columns * 0.6)
# rotate this mark by 45 degrees anticlockwise (optional)
# watermark.rotate!(-45)
# using composite
# place the watermark in the center of the image
# default 'compose over' overlays the watermark on the background image
# SoftLightCompositeOp darkens or lightens the colors, dependent on the source color value.
# If the source color is lighter than 0.5, the destination is lightened.
# If the source color is darker than 0.5, the destination is darkened, as if it were burned in.
# The degree of darkening or lightening is proportional to the difference between the source color and 0.5.
# If it is equal to 0.5, the destination is unchanged.
# Painting with pure black or white produces a distinctly darker or lighter area, but does not result in pure black or white.
# img1 = img.composite(watermark, Magick::SouthEastGravity, Magick::SoftLightCompositeOp)
# using overlay
# We define which image to composite (img is the source image we want watermarked), then we define where that watermark should start
# finally we say that the watermark should be composed over the top of the source image with OverCompositeOp
img1 = img.composite(watermark, Magick::SouthEastGravity, 25, 25, Magick::OverCompositeOp)
# save the watermarked image
img1.write("/Users/i851546/watermarked_file.jpg")
# using watermark
# place the watermark in the center of the image with gravity
# watermark the image with 20% brightness and 30% saturation
# img2 = img.watermark(watermark, 0.2, 0.3, Magick::CenterGravity)
# save the watermarked image
# img2.write("/home/aditya/Pictures/wm_old_england_pic_image_watermark.jpg")
# using dissolve
# add watermark with 40% opacity for watermark, 100% opacity for image and position is center
# img3 = img.dissolve(watermark, 0.4, 1, Magick::CenterGravity)
# save the watermarked image
# img3.write("/home/aditya/Pictures/wm_old_england_pic_image_dissolve.jpg")
# Watermarking image with text using ImageMagick 'composite', 'watermark' and 'dissolve'
require "RMagick"
# Read the image in the memory with RMagick
img = Magick::Image.read("/home/aditya/Pictures/old_england_pic.jpg").first
# Create a new image in memory with transparent canvas
# size of this 'mark' image is same as original image which we want to watermark
mark = Magick::Image.new(img.rows, img.columns) {self.background_color = "none"}
draw = Magick::Draw.new
# draw is used to add elements to an image like text
draw.annotate(mark, 0, 0, 0, 0, "creative commons") do
# place the text in the centre of the canvas
draw.gravity = Magick::CenterGravity
# set text height in points where 1 point is 1/72 inches
draw.pointsize = 100
draw.font_family = "Times" # set font
draw.fill = "black" # set text color
draw.stroke = "none" # remove stroke
end
# rotate this mark by 45 degrees anticlockwise (optional)
# if we do not specify 'background_color' on 'mark' then on rotation the background color will be black.
# we want it to be transparent.
mark = mark.rotate(-45)
# using composite
# place the watermark in the center of the image
# default 'compose over' overlays the watermark on the background image
# SoftLightCompositeOp darkens or lightens the colors, dependent on the source color value.
# If the source color is lighter than 0.5, the destination is lightened.
# If the source color is darker than 0.5, the destination is darkened, as if it were burned in.
# The degree of darkening or lightening is proportional to the difference between the source color and 0.5.
# If it is equal to 0.5, the destination is unchanged.
# Painting with pure black or white produces a distinctly darker or lighter area, but does not result in pure black or white.
img1 = img.composite(mark, Magick::CenterGravity, Magick::SoftLightCompositeOp)
# save the watermarked image
img1.write("/home/aditya/Pictures/wm_old_england_pic_text_composite.jpg")
# using watermark
# place the watermark in the center of the image with gravity
# watermark the image with 20% brightness and 50% saturation
img2 = img.watermark(mark, 0.2, 0.5, Magick::CenterGravity)
# save the watermarked image
img2.write("/home/aditya/Pictures/wm_old_england_pic_text_watermark.jpg")
# using dissolve
# add watermark with 25% opacity for watermark, 50% opacity for image and position is center
img3 = img.dissolve(mark, 0.25, 0.5, Magick::CenterGravity)
# save the watermarked image
img3.write("/home/aditya/Pictures/wm_old_england_pic_text_dissolve.jpg")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment