ImageMagick chaining woes - imagemagick

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.

Related

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

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.

imagemagick nested gravity centering

I am trying to composite two images with gravity, and then position them within a larger image at a geometry.
When I try
magick -size 1045x837 xc:blue \( -size 345x437 xc:red \( -size 275x417 xc:white -resize 345x437 -gravity center \) -composite \) -geometry +26+53 -composite test-y.png
I get
and when I do
magick -size 1045x837 xc:blue \( -size 345x437 xc:red \( -size 275x417 xc:white -resize 345x437 \) -composite \) -geometry +26+53 -composite test-x.png
I get
I think this involves clone and related, maybe similar to this answer, but I just can't find the combo.
What do I need to do, to get the white centered within the red, and geometrically placed in the upper-left corner?
It's not clear exactly what you want, but I think you are falling foul of the fact that -gravity is a "setting". As such, it remains set until changed, so you probably want this, where I reset the gravity to NorthWest before the final composite:
magick -size 1045x837 xc:blue \( -size 345x437 xc:red \( -size 275x417 xc:white -resize 345x437 -gravity center \) -composite \) -gravity northwest -geometry +26+53 -composite result.png
You might find -extent a simpler way to fill out the white to a given size using a red background:
magick -size 1045x837 xc:blue \( -size 275x417 xc:white -resize 345x437 -background red -gravity center -extent 345x437 \) -gravity northwest -geometry +26+53 -composite result.png

Imagick resize but don't save resized

I want to resize watermark.png but i dont want to chnage original watermark.png ! i want to get watermark.png and resize it and add on image.png but
I don't want to save resized file in watermark.png!
composite -gravity southeast -geometry +10+10 -dissolve 70% watermark.png image.png image-watermarked.png
Assume you have two images, both 400x250 like this:
background.png
watermark.png
Then you probably want something like this:
convert background.png \( watermark.png -resize 100x \) -gravity southeast -composite result.png
Hopefully you can see that the parentheses prevent the -resize from applying to the background.
If you want to use a dissolve, you can do that like this:
convert background.png \( watermark.png -resize 100x \) \
-gravity southeast -define compose:args=20% \
-compose dissolve -composite result.png
I found it ... ill make another resized image and delete it...
convert watermark.png -scale 30% watermark2.png
composite -gravity southeast -geometry +10+10 -dissolve 70% watermark2.png image.png image-watermarked.png
rm -rf ./man/tnx-v2.png
another answer (this has -colorize for dissolve)
convert image.png \( watermark.png -scale 50% -colorize 50% \) -gravity southeast -composite image-watermarked.png

cropped image and layer it at some position in a second canvas-imagemagick

I would like to crop image and flatten it over a second canvas.
for instance, my image1 crop would be=>
-crop 20x800+450+0
and I would like to put it at the position of my second image.
-page 0+0
this just doesn't work=>
convert -page 0+0 image1.jpg -crop 20x800+450+0 -layers flatten image2.jpg
how could I do this ?
thanks in advance
I find your question very hard to understand, but let's assume you have this:
# a canvas
convert -size 1200x1200 xc:gray canvas.png
and an image:
convert -size 1000x1000 gradient:red-blue image1.png
Then I am guessing you want this:
convert canvas.png \( image1.png -crop 20x800+450+0 \) -composite result.png
That works because the default -gravity is NorthWest, if you wanted it elsewhere you can do things like this:
convert canvas.png \( image1.png -crop 20x800+450+0 \) -gravity east -composite result.png
Or this, with an up-down offset from the South side:
convert canvas.png \( image1.png -crop 20x800+450+0 \) -gravity south -geometry +0+50 -composite result.png
Or with a left-right offset from the West side:
convert canvas.png \( image1.png -crop 20x800+450+0 \) -gravity west -geometry +100 -composite result.png
Or maybe you want to rotate the extracted crop before overlaying it:
convert canvas.png \( image1.png -crop 20x800+450+0 -rotate 90 \) -gravity north -geometry +0+20 -composite result.png
Or maybe you were trying to do it this way:
convert canvas.png -respect-parentheses \( -page +300+50 image1.png -crop 20x800+450+0 \) -layers flatten result.png

Crop 40/60 with ImageMagick

I know how to use -crop 50%x100% to split an image in half (50/50) but is there a way to crop into 40/60?
If I use
convert -crop 40%x100% in.jpg out.jpg
I end up getting:
out-0.jpg // 40%
out-1.jpg // 40%
out-2.jpg // remaining %
convert in.jpg -gravity west -crop 40%x100% +repage out1.jpg
convert in.jpg -gravity east -crop 60%x100% +repage out2.jpg
I'm sure there is a more complex way, but I would just do it in two commands.
You can do it in a single command like this:
convert in.jpg \
\( +clone -gravity east -crop 60x100% +repage +write east.jpg +delete \) \
-gravity west -crop 40x100% +repage west.jpg

Resources