Image Magick blend 2 cropped/resize images together in the middle - imagemagick

Hey all I have this code below that takes 2 images and merges them together with it fading in the center:
convert testingl.jpg -gravity West ^
testingr.jpg -gravity East ^
blend_mask.png -extent 1080x440 -gravity center -composite bothBlended.jpg
The above produces this:
Using this mask:
Taken from these 2 images (lowered res to fit on here):
testingl.jpg (original size 1224 x 1632)
testingr.jpg (original size 828 x 1792)
This code works great as-is. Does what I need it to do but with one exception - I am wanting to get more of each image into it. Like resize the image, crop from the center the image then take that and blend it. I need to keep the same 1080 x 440 overall size.
Do that with both would look something like this:
UPDATE 1
When running #fmw42's code:
convert ^
( testingl.jpg -resize 1080x440^ -gravity West -extent 1080x440 ) ^
( testingr.jpg -resize 1080x440^ -gravity East -extent 1080x440 ) ^
blend_mask.png -composite abc.jpg
I get this:

Your ImageMagick command does not generate your proposed output image. I think it is missing a resize and parentheses.
In Unix syntax, I need the following to get your output.
convert \
\( testingl.png -resize 1080x440^ -gravity West -extent 1080x440 \) \
\( testingr.png -resize 1080x440^ -gravity East -extent 1080x440 \) \
blend_mask.png -composite bothBlended1.jpg
or perhaps you want
convert \
\( testingl.png -resize 1080x440^ -gravity West -extent 1080x440 \) \
\( testingr.png -resize 1080x440! -gravity East -extent 1080x440 \) \
blend_mask.png -composite bothBlended2.jpg

Related

split image horizontally into two unequal parts?

I have the following image:
I would like to use ImageMagick to split it horizontally into two unequal parts of 40-60% (L-R). How do I do this?
You can do that as follows in ImageMagick 6. Read the image into MPR memory and delete the original. Then use the MPR copy to crop 40% once with gravity west and crop 60% again with gravity east (that is 40% from the left side and then 60% from the right side). Write those images and then exit with no output, i.e., null:
Unix Syntax:
convert red_rect.png +repage -write mpr:img +delete \
\( mpr:img -gravity west -crop 40x100%+0+0 +repage +write left.png \) \
\( mpr:img -gravity east -crop 60x100%+0+0 +repage +write right.png \) \
null:
For Windows,
convert red_rect.png +repage -write mpr:img +delete ^
( mpr:img -gravity west -crop 40x100%+0+0 +repage +write left.png ) ^
( mpr:img -gravity east -crop 60x100%+0+0 +repage +write right.png ) ^
null:
(In .bat file, double the % to %%)
(For ImageMagick 7, change convert to magick)
Left:
Right:
Just for fun, a slightly different version:
load image
make clone, crop left side and save, delete clone
revert to original, crop right side and save
magick GhLiu.png +repage \( +clone -crop 40x100%+0+0 +repage +write left.png +delete \) -gravity east -crop 60x100%+0+0 +repage right.png

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

ImageMagick, set relative size in chain of convert operators

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...

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