Add watermark after tile in ImageMagick - command-line

I got this working code for tiling an image in ImageMagick:
convert D:\tile.jpg -resize 300x300 -write mpr:tiler +delete ^
-size 900x900 tile:mpr:tiler -sharpen 0x2 D:\tiles.jpg
But now i found out that i need to add watermark in the same process. I got this working code for that:
tiles.jpg Wartermark.png -gravity southeast -geometry +0+0 -composite D:\tile-watermark.jpg
But how do i process those two in one line?

Related

ImageMagick: composite 4 edges from one image into another, side-by-side

I'm using Perl 5.16 with ImageMagick 6.8 (probably old by now, but it works :). I'm trying to extract all 4 edge regions from one image and composite them into another image, side-by-side, oriented vertically.
I can extract edges and rotate them, but I can't get the offset in the target image right. The edges end up on top of each other. I tried x=>$marg, translate=>"$marg,0", geometry with an offset: no dice. Variables: $marg = the edge width, $im = source, $im2 = target, $ext = extracted region
# left edge
$ext=sprintf("%dx%d+%d+%d",$marg,$h,0,0);
$res=$im2->Composite(image=>$im,compose=>Over,extract=>$ext);
# top edge
$ext=sprintf("%dx%d+%d+%d",$w,$marg,0,0);
$geo=sprintf("%dx%d+%d+%d",$marg,$h,0,0);
$res=$im2->Composite(image=>$im,compose=>Over,extract=>$ext,rotate=>90,translate=>"$marg,0");
I haven't used the PerlMagick bindings for years, but I imagine you would want to create each of your 4 edge strips and then use +append to lay them out horizontally, side-by-side. Here's a crude example, just in Terminal:
magick -size 20x50 xc:red xc:lime xc:blue +append result.png
More specifically addressing your question, and starting with this image:
that would look like this:
#!/bin/bash
magick ~/sample/images/blocks-RGB.png -resize 100x100 +repage -write MPR:orig +delete \
\( MPR:orig -gravity northwest -crop 30x+0+0 \) \
\( MPR:orig -gravity northwest -crop x30+0+0 -rotate 90 \) \
\( MPR:orig -gravity southwest -crop x30+0+0 -rotate 90 \) \
\( MPR:orig -gravity northeast -crop 30x+0+0 \) \
+append result.png
Hopefully you can see the parallels with PerlMagick. The first line creates a copy of the image to work with, the second line crops the left edge, the third line crops the top edge, the fourth line crops the bottom edge, the fifth line crops the right edge and the last line appends the four cropped pieces side-by-side.

Imagemagick commands to resize, rotate, wrap, and combine images

I'm currently using Photoshop to resize, rotate randomly, and wrap images randomly to create this type of montage....
Comic Covers
I got to thinking that kind of thing should be doable in Imagemagick. I know how to use all of the commands separately, and I can do random rotations and wraps using BASH, but getting a single image out of individual images is eluding me.
Assume that the source pictures are different sizes but should be resized to 250px wide. The images will be named image1.jpg, image2.jpg, etc. Also assume that the destination should be 1000x1000px. Depending on how many pictures I have, the whole 1000x1000 image may not be covered - I understand this. I mainly use BASH, but I have several different environments and shells available to me.
Using ImageMagick 6 or 7, if you have enough memory to read in all your images at once you can resize them, randomly rotate them, and place them all in random locations on a 1000x1000 canvas with a command like this...
convert granite: -duplicate 11 -resize 250x \
-background none -gravity center -extent 1000x1000 \
-distort SRT "%[fx:rand()*45-22.5]" -virtual-pixel tile \
-distort affine "%[fx:w/2],%[fx:h/2] %[fx:rand()*w],%[fx:rand()*h]" \
-flatten result.png
That uses the ImageMagick built-in image "granite:" duplicated 11 more times. Replace "granite: -duplicate 11" with a list of your input files.
It starts by resizing them all to 250 pixels wide, then placing them each in the center of a 1000x1000 transparent canvas.
The real work is done in the distort operations. First "-distort SRT" rotates each image a random amount between -22.5 and +22.5 degrees. Then the "-distort affine" relocates each image to a random location within the canvas. Any part of an image going beyond the canvas will be rolled back into the opposite side. That makes the result suitable for tiling.
This command flattens everything onto a transparent background wherever it might show between the images. Add "-background blue" just before the "-flatten" operation to change the background to blue, for example.
This works on my IM 6 in bash. For IM 6 in Windows change the continued line backslashes "\" to carets "^". For IM version 7 change "convert" to "magick".
Here is a bash Imagemagick 6 script that takes a list of images. You can replace it with your images. It uses subshell processing to avoid needing to write temporary images to disk. It saves the images in a miff: format as one file from the loop. Then it pipes the multipage miff: file to -layers merge, which overlays the images onto the 1000x1000 transparent base image. For Imagemagick 7, replace convert with magick.
list="lena.jpg barn.jpg mandril3.jpg zelda1.jpg"
convert -size 1000x1000 xc:none result.png
(
for img in $list; do
angle=`convert xc: -format "%[fx:round(-22.5+45*(rand()))]" info:`
xoff=`convert xc: -format "%[fx:round(1000*rand())]" info:`
yoff=`convert xc: -format "%[fx:round(1000*rand())]" info:`
#echo >&2 "angle=$angle; xoff=$xoff; yoff=$yoff"
convert "$img" -resize 250x -background none -rotate $angle -set page +${xoff}+${yoff} miff:-
done
) | convert result.png - -layers merge +repage result.png
If you have enough resources to hold all the images at once, then you can also do it in one command line as follows:
convert -size 1000x1000 xc:none \
\( lena.jpg barn.jpg mandril3.jpg zelda1.jpg -virtual-pixel none -background none \
+distort SRT "0,0 %[fx:250/w] %[fx:-22.5+45*rand()] %[fx:rand()*1000],%[fx:rand()*1000]" \) \
-layers merge +repage result.png
Cool, I'll try fmw42's script, but this is a script I came up with. It generates temporary files (which it deletes) and several convert commands, but it does work....
# Create blank montage...
convert -size 750x750 xc:black montage.jpg
for file in $(ls hall*.jpg | grep -v halloweencovers.jpg); do
echo $file
angle=$RANDOM; let "angle %= 32"; let "angle = angle - 16"; let "angle = angle * 5"
offsetx=$RANDOM; let "offsetx %= 75";let "offsetx = offsetx * 10"; offsetx="+$offsetx"
offsety=$RANDOM; let "offsety %= 75";let "offsety = offsety * 10"; offsety="+$offsety"
# Create blank image...
convert -size 750x750 xc:transparent blank.png
# create 250px image and rotate....
convert $file -resize 250x -alpha set -background none -rotate $angle out.png
# add 250px image to blank 750x750 canvas
convert blank.png out.png -composite output.png
# offset and wrap blank canvas with output image
convert output.png -roll ${offsetx}${offsety} output2.png
# merge montage with offset image
convert montage.jpg output2.png -composite montage.jpg
# clean up
rm -f out.png output.png output2.png blank.png
done

Imagemagick to merge and mask images

I recently started working with Imagemagick and I had a problem that needs to be solved.
I have such a thumbnail.
And there is such a mask.
I need to get this image on the output.
I looked through this link http://www.imagemagick.org/Usage/thumbnails/ I tried everything there, but I did not get success ((
I hope you will help me.
This should give you a result very much like your example...
convert mask.png image.png -gravity north -composite \
mask.png -compose copyopacity -composite result.png
You can adjust the positioning of the overlay by including something like "-geometry -10+0" before the first composite.
To use this command in Windows you'll need to change that continued line backslash "\" to a caret "^".
In Imagemagick, you should extract the alpha channel of your mask and put that into the alpha channel of your other image. So try
(unix syntax)
convert KlQZ6.png \( 7k9a9.png -alpha extract \) -alpha off -compose copy_opacity -composite result.png
(windows syntax)
convert KlQZ6.png ( 7k9a9.png -alpha extract ) -alpha off -compose copy_opacity -composite result.png

Image reflection using imagemagick command line?

Original Image
convert image.jpg -scale 310x496\! scaled.png
convert scaled.png +clone -flip -crop 310x150 -compose Dst -composite out/shadow.png
convert -size 310x150 -alpha set gradient:rgba\(255,0,255,0.6\)-rgba\(255,255,0,0.50\) out/grad.png
convert out/shadow.png out/grad.png -compose Dst_Out -composite out/shadow_gradiented.png
convert shadow_gradiented.png out/shadowed.png -append out/final.png
I am getting following output
But I want my output to be like following.
What am i doing wrong? Suggest me.
Additional:
Is there any way I can make all in one command.
Not certain what you are trying to achieve, but this might get you started:
convert wolf.jpg -scale 310x496\! \
\( +clone -flip -crop x150+0+0\! -alpha set -channel A -fx "0.6" \) -append result.png
If you want the alpha tailing off, try a formula that is a function of j (the distance from the top of the reflection) and h (the total height of the reflection).
convert wolf.jpg -scale 310x496\! \
\( +clone -flip -crop x150+0+0\! -alpha set -channel A -fx "0.8-(0.6*j)/h" \) -append result.png
If you want the code to be a bit more generic, and less dependent on actual sizes, you could change the height of the reflection to, say 1/4, of the height of the original (note the change from convert to magick)
magick wolf.jpg -scale 310x496\! \
\( +clone -flip -crop "x%[fx:h/4]+0+0\!" -alpha set -channel A -fx "0.8-(0.6*j)/h" \) -append result.png

Imagemagick create thumbnail

I have an image that is 200x200 I want to trim the white space, and make it into an icon that is 32x32.
convert img.png -trim -resize 32x32 imgi.png
This line of code does not work, it will not make the canvas a uniform 32x32. I have tried taking it and adding -canvas 32x32 or -extend 32x32 but then the script does not work at all.
I am doing this from PHP with exec so I do not get an error back.
You can pad to tweak dimensions of the image after cropping. As of ImageMagick version 6.3.2 you can use -extent option to modify end result.
convert img.png -trim -resize 32x32 -gravity center -extent 32x32 imgi.png
For earlier versions it is necessary to play with -border, -crop and +repage.
convert img.png -trim -resize 32x32 -bordercolor white -border 16 -gravity center -crop 32x32+0+0 +repage img.png;