How to use imagemagick to give background and dimensions to my screenshot? - imagemagick

I am new to Image Magick but am trying to get it to convert my image to a size and background pleasant for Twitter. The qualities I'm going for are as follows:
my overall canvas size 16:9, so about 1200x675
my actual screenshot centered and about 3/4 the width
background texture of my choosing
My latest attempt is with the following, but it doesn't seem to actually make any noticeable changes.
convert ds.png -adaptive-resize 1200 -size 1200x675 -texture ~/Pictures/DoctorWho/horizon.jpg -gravity Center ds.png

In Imagemagick 6, Unix syntax, if you just want to resize a small background image, then try
convert \( background.suffix -resize 1200x675 \) \( foreground.suffix -resize x506 \) -gravity center -compose over -composite result.suffix
If you want to crop a large image for the background, then try
convert \( background.suffix -gravity center -crop 1200x675+0+0 +repage \) \( foreground.suffix -resize x506 \) -gravity center -compose over -composite result.suffix
If you need to tile out a small image for the background, then
convert \( -size 1200x675 tile:background.suffix \) \( foreground.suffix -resize x506 \) -gravity center -compose over -composite result.suffix
For Windows remove the \ from the parentheses.
For Imagemagick 7, replace convert with magick.

Related

ImageMagick add watermark proportionally

I want to add a watermark logo.png to the nature.jpg with ImageMagick, I use the following commands:
magick nature.jpg logo.png -gravity southeast -geometry +10+10 -composite nature-wm.jpg
convert nature.jpg -resize 50% nature-50%.jpg
magick nature-50%.jpg logo.png -gravity southeast -geometry +10+10 -composite nature-50%-wm.jpg
As you can see, the watermark is smaller when adding it to an image which has large width and height, but this is not what I expected, what I expected is that the watermark can always occupy a fixed percentage of the main image, anyone know how to do this?
logo image
original image
Original image with watermark added
50% width and height of the original image(nature-50%.jpg)
nature-50%.jpg with watermark added(the logo in nature-50%.jpg looks bigger than in the original image)
Update
I want to rotate the watermark, but my approach is not what I expected, it's weird
magick nature.jpg -set option:logowidth "%[fx:int(w*0.25)]" \
\( logo.png -resize "%[logowidth]x" -rotate 45 \) \
-gravity southeast -geometry +10+10 -composite result.jpg
I would do that by loading the background, calculating the width of the logo relative to that (here I used 0.25 as my factor), then loading the logo and resizing before compositing:
magick background.jpg -set option:logowidth "%[fx:int(w*0.25)]" \
\( logo.png -resize "%[logowidth]x" \) \
-gravity southeast -geometry +10+10 -composite result.jpg
Regarding changing the transparency, per your comment, you could make the logo 30% opaque like this:
magick background.jpg -set option:logowidth "%[fx:int(w*0.25)]" \( logo.png -resize "%[logowidth]x" -channel A -fx "u*0.3" \) -gravity southeast -geometry +10+10 -composite result.jpg

Imagemagick create image and place image inside with max size

I have trimmed .pngs and need them placed on a canvas (3000x3000 px) BUT with a max size.
convert -size 3000x30000 xc:transparent test.png -gravity south -composite -size 2200x2200 result.png
The code I have now works, but the sizing of the image is off. My canvas is 3000x3000px as intended, but the image placed on the canvas doesn't have the correct size. It should have a max width/height of 2200px and if possible be scaled up, if they are to small in height.
This ImageMagick command will resize the input image to 2200 pixels on the longer side while maintaining its aspect, then create a 3000x3000 transparent canvas, then swap the input image and the canvas, and finish by compositing the resized input image onto the transparent canvas...
convert input.png -resize 2200x2200 \
-size 3000x3000 xc:none +swap -gravity south -composite result.png
For Windows change that continued line backslash "\" to a caret "^". For ImageMagick v7 use "magick" instead of "convert".
Unix syntax:
convert -size 3000x30000 xc:transparent \( test.png -resize 2200x2200 \) -gravity south -composite result.png
Windows syntax:
convert -size 3000x30000 xc:transparent ( test.png -resize 2200x2200 ) -gravity south -composite result.png
Why not use -extent?
convert input.jpg -resize 2200x2200 -background none -gravity south -extent 3000x3000 result.png

How to scale and crop a folder of images?

I've been looking at the documentation for imagemagick and looking at examples of scripts others have made, but I haven't been able to get this working.
The goal is to have imagemagick scale, crop, and save (appending the dimensions to the file names) multiple resized images of various aspect ratios.
For example, a folder containing Image1.png and Image2.png would result in:
Image1_1571x2646.png, Image1_1350x2150.png, Image1_1281x2039.png
Image2_1571x2646.png, Image2_1350x2150.png, Image2_1281x2039.png
Visual aid:
The animation above shows the simplest examples: a 1:1 square, a vertical rectangle, and a horizontal rectangle.
The images should scale to fit the longest dimension of the rectangle, and then crop any leftover pixels. The scaling and cropping should be done relative to the image centers.
Here's what I have so far (using macOS Terminal) but it doesn't work:
convert *.png -path /Users/user/Resized \
\( +clone -resize "1571x2646^” -gravity center -crop 1571x2646+0+0 +repage resultimage -write 1571x2646.png +delete \) \
\( +clone -resize "1350x2150^” -gravity center -crop 1350x2150+0+0 +repage resultimage -write 1350x2150.png +delete \) \
-resize "1281x2039^” -gravity center -crop 1281x2039+0+0 +repage resultimage 1281x2039.png
I'm not sure if I should use mogrify or convert, but if I use mogrify clone gives an error. I'm also not sure if multi-line instructions need to be put into a .sh file or something. The ^ denotes the dimension that should take priority (the larger one). I believe -gravity center is supposed to keep scaling and cropping relative to the image centers.
With Imagemagick, you must use convert. Mogrify cannot handle the parenthesis process and clones, nor can it write multiple outputs for a given input. The ^ is the correct way and -gravity center is correct. You will have to loop over each input image. I do not think you can use wild cards to process more than one image at a time with this type of command. I think -path is only for mogrify.
I would write a loop over each of your input images (bash unix syntax):
cd
cd /Users/user/Resized/
list=`ls`
for img in $list; do
name=`convert "$img" -format "%t" info:`
convert "$img" \
\( -clone 0 -resize "1571x2646^" -gravity center -crop 1571x2646+0+0 +repage +write ${name}_1571x2646.png +delete \) \
\( -clone 0 -resize "1350x2150^" -gravity center -crop 1350x2150+0+0 +repage +write ${name}_1350x2150.png +delete \) \
\( -clone 0 -resize "1281x2039^" -gravity center -crop 1281x2039+0+0 +repage +write ${name}_1281x2039.png +delete \) \
null:
done
The above assumes that your input images have no spaces in their names.
I have changed from +clone to -clone 0, since I am not sure if you change aspect ratio from output to output whether that will cause problems. You can try both ways and see which looks best.

add image to transparent section

I have an image with a couple of transparent boxes. I need to insert some specific images in to the transparent boxes. I tried several convert commands but couldn't end up with a solution.
I am using Windows 10 and imagemagick is working on my CLI with no issues. Hope someone can point me into the right direction.
Let's say this 500x400 image is your starting image and it has transparent holes in it at 10,10 and 250,250.
Now, let's say you have two Mr Beans, bean1.jpg and bean2.jpg like this:
Let's lay it out on a red background so you can see what is going on. We'll resize bean1.jpg and place him in the area of the top-left transparent hole, then we'll set up bean2.jpg for the bottom-right transparent hole:
convert -size 500x400 xc:red \
\( bean1.jpg -resize 101x101! -geometry +10+10 \) -composite \
\( bean2.jpg -resize 131x131! -geometry +250+250 \) -composite \
result.png
Now let's do that again, but this time, overlay the original image so the Beans peek through it:
convert -size 500x400 xc:red \
\( bean1.jpg -resize 101x101! -geometry +10+10 \) -composite \
\( bean2.jpg -resize 131x131! -geometry +250+250 \) -composite \
image.png -composite result.png
On Windows, you have to change the backslashes into carets, so \( becomes ^( and \) becomes ^).

ImageMagick chaining woes

I can't figure out how to properly chain commands in ImageMagick
Things that do what I expect in isolation :
Resize and then crop
$ convert input.jpg -resize '400x400>' -gravity center -crop 300x400+0+0 +repage output.jpg
Apply overlay
$ convert -composite input.jpg overlay.png output.jpg
Annotate
$ convert input.jpg -annotate +55+357 'The text I want' output.jpg
I've had limited success in combining these together for instance :
$ convert \( input.jpg -resize '400x400>' -gravity center -crop 300x400+0+0 +repage \) mask.png -composite output.jpg
Resizes the image and crops it, then applies my overlay. However regardless of what I try I can't then get the annotation to appear.
What I want to do is something like :
$ convert \( input.jpg -resize '400x400>' -gravity center -crop 300x400+0+0 +repage \) mask.png -composite \( -annotate +55+357 'The text I want' \) output.jpg
Thanks.
Answering my own question :
Adding -gravity NorthWest before the annotation solves the problem.
$ convert input.jpg -resize '400x400>' -gravity center -crop 300x400+0+0 +repage mask.png -composite -gravity NorthWest -annotate +55+357 'The text I want' output.jpg
I believe this effectively resets the 0,0 after the crop / resize so the annotation appears where it is expected to.
Note that you don't need the parenthesis either.
Credit to snibgo on the ImageMagick forum.

Resources