Skip to content

Instantly share code, notes, and snippets.

@vagnerzampieri
Created June 9, 2018 02:00
Show Gist options
  • Save vagnerzampieri/067393ab2c56d1c71bbf45802fb9ed4d to your computer and use it in GitHub Desktop.
Save vagnerzampieri/067393ab2c56d1c71bbf45802fb9ed4d to your computer and use it in GitHub Desktop.
marking_photo
# Watermarking image with another image using Imagemagick 'composite', 'watermark' and 'dissolve'.
require "rmagick"
Dir.entries("./target").each_slice(7) do |group|
group.each do |target_photo|
next if target_photo == "." || target_photo == ".."
# This is necessary to don't explode your memory
GC.start
GC.disable
# Read the image in the memory with RMagick
img = Magick::Image.read("./target/#{target_photo}").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
mark = Magick::Image.read("./watermark.png").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.3, img.columns * 0.3)
# 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::HardLightCompositeOp)
# save the watermarked image
img1.write("./new-photos/#{target_photo}")
# 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")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment