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
Related
We need to put 'information bar's to thousands of image files. For like a week or so i'm trying to learn imagemagick but i just couldn't figure this many elements out so i wanted to ask for a help here.
I get the idea of '-/+append'ing elements and swapping between them but when it comes to 3x3 matrix cells and text/image mixings, i just can't do it. As an example, i can get the 3 rows appended and a column next to it but i can't get to the next step of 'appending 2 more rows together then put them as a column block again' because when i try, all those append gets right or bottom as a whole image.. Well, you will get the idea when you see my brief image below..
magick.exe -size 150x100 -gravity center caption:"txt2" caption:"txt3" caption:"txt4" \
-append -size 94x294 xc:white -border 3 -swap 0,1 \
+append outoutout.jpg
FYI, height/width of rows/columns are there just for example not important.. And here comes MSPaint skills:
You have to create each section separately using parenthesis processing. Then if you want append them appropriately. Alternately, you can create a background image and compose ... -composite each image into its correct location.
Here is an example in ImageMagick using the second method.
Unix Syntax:
magick -size 400x400 xc:white \
\( barn.jpg -crop 400x200+0+0 +repage \) \
-geometry +0+0 -compose over -composite \
\( -size 100x200 xc:white -shave 5x5 -bordercolor black -border 5 \) \
-gravity northwest -geometry +0+200 -compose over -composite \
\( -size 100x200 -background white -gravity center -fill black \
-font Candice label:"Text1\n\nText2\n\nText3" \) \
-gravity northwest -geometry +100+200 -compose over -composite \
\( -size 100x200 -background skyblue -gravity center -fill red \
-font Arial label:"First_line\n\n\nSecond_line" \) \
-gravity northwest -geometry +300+200 -compose over -composite \
result.png
See for example:
parenthesis processing
appending
convert ... -composite
I'd need to set the size relative to the size already set. I'd need it because my flow involves defining primitives inside other ones. Eg
convert -size 200x100 xc:black \( -size 30x40% xc:red \) -gravity West -composite out.png
That 30x40% is not working this way, it becomes pixels 30x40. I can achieve this specific goal in the first example by using resize
convert -size 200x100 xc:black \( xc:red -resize 30x40% \) -gravity West -composite out.png
In this second version, xc:red inherits the size 200x100, so -resize works as expected. Though the size of further/inner primitives are not reduced to 60x40, it remains 200x100, so in the third example, the green rectangle has orientation landscape and not portrait
convert -size 200x150 xc:blue \
\( xc:red -resize 50x100% \
\( xc:green -resize 40% \) \
-gravity Center \
-composite \
\) \
-gravity West \
-composite \
out.png
So green area is 80x60 pixels, 40% of 200x150. I'd like to somehow reset the size to the size of xc:red after resize by the time I'm introducing xc:green.
I think you are trying to create canvases whereby each one is a percentage of the the size of the previous one. There may be an easier way, but you could save each canvas (and implicitly its size) in a MPR "Magick Persistent Register" (named lump of RAM) as you create it, then recall the latest one and overwrite it each time you want to do something relative to that:
convert -gravity west -size 200x100 xc:black -write MPR:S \
\( MPR:S -resize 30x40% -fill red -colorize 100 -write MPR:S \) -composite \
\( MPR:S -resize 50x50% -fill blue -colorize 100 -write MPR:S \) -composite \
\( MPR:S -resize 50x50% -fill lime -colorize 100 \) -composite result.png
Alternatively, you could let your bash/POSIX shell do it for you inside an "arithmetic expression":
W=200
H=100
convert -gravity west -size ${W}x${H} xc:black \
\( -size $((W=W*30/100))x$((H=H*40/100)) xc:red \) -composite \
\( -size $((W=W*50/100))x$((H=H*50/100)) xc:blue \) -composite \
result.png
Be aware that the shell only deals with integer maths, so it's not going to end well if you aim for 50% of 25 pixels...
Hi I would like to create a mask image with "special corners" I am calling them special because I don't really know how to call them in english here is what I would like to achieve:
what I am using now is
convert xc:black -size 300x300 -fill white -draw "roundrectangle 3,3,296,296,5,5"
but this gives me rounded corners. Thank you in advance for any suggestions.
Here's one way of doing it.
convert -size 300x300 xc:none \
-shave 10 -bordercolor black -border 10 \
-fill black -draw "polyline 0,0 30,0 0,30" \
\( +clone -flip \) -gravity north -composite \
\( +clone -flop \) -gravity south -composite -background white -flatten result.png
That says... "Draw a rectangle your full size and transparent, shave 10 pixels off all round and add a 10 pixel black border (easier than doing the maths and making a 280x280 and adding 10 on each side). Draw a triangle in the top-left. Copy the whole shape and flip it and draw it on top of the original. Copy the whole shape and flop it and draw it again on the original. Now make all the transparent areas white."
Here is another way - maybe a little easier. Draw the original square, then copy it, enlarge it by square-root(2) (i.e. 141%), thicken the borders, rotate 45 degrees and composite it onto itself. Kinda depends how your brain works!
magick -size 300x300 xc:none -shave 10 -bordercolor black -border 10 \
\( +clone -scale 142% -shave 30 -border 30 -rotate 45 \) \
-gravity center -composite -background white -flatten result.png
Here is a link to a page showing how to do what you want along with other effects https://www.imagemagick.org/Usage/thumbnails/#rounded
This is the code from the page:
convert thumbnail.gif -alpha set -compose DstOut \
\( -size 20x15 xc:none -draw "polygon 0,0 0,14 19,0" \
-write mpr:triangle +delete \) \
\( mpr:triangle \) -gravity northwest -composite \
\( mpr:triangle -flip \) -gravity southwest -composite \
\( mpr:triangle -flop \) -gravity northeast -composite \
\( mpr:triangle -rotate 180 \) -gravity southeast -composite \
corner_cutoff.png
You should check out the examples as there is another method you could use for smaller images.
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
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.