Combined Multiple Commands in Imagemagick - imagemagick

I'n very new to imagemagick and i need to learn how could i combined these two commands to make it work.
I need to add background image to a transparent image. I tried to combine these two commands but was not successful.
magick mogrify -path Output_Path -trim -filter Triangle -define filter:support=2 -thumbnail 450x450 -gravity center -extent 500x500 -unsharp 0.25x0.25+8+0.065 -dither None -posterize 136 -quality 82 -define jpeg:fancy-upsampling=off -define png:compression-filter=5 -define png:compression-level=9 -define png:compression-strategy=1 -define png:exclude-chunk=all -interlace none -colorspace sRGB -strip Transparent_Image_Path
magick Background_Image.png Transparent_Image.png -composite output.jpg
Result Should Be Like This :
Image Reference
Thanks in advance!

There are several possible syntaxes, depending on how your brain likes to work ;-)
What you need to bear in mind when trying to understand this is that all processing operations, e.g. -crop or -resize apply to all loaded images. So, you either need to load images that need processing and process them before loading images you don't want affected, or you need to restrict processing to certain images only... using parenthesised "aside" operations.
You can proceed like this:
load transparent foreground image first and process it before loading the background image, then
load background image, then
exchange the order and composite
That looks like this:
magick FOREGROUND.PNG -resize ... -crop ... \
BACKGROUND.PNG \
+swap -composite RESULT.JPG
Alternatively, you could:
load the background image, then
load the foreground image and process it "aside" within parentheses so that the background image is not affected
composite
That looks like this:
magick BACKGROUND.PNG \
\( FOREGROUND.PNG -resize ... -crop ... \) \
-composite RESULT.JPG
If you need to independently process both background and foreground images prior to compositing, use:
magick \
\( BACKGROUND.PNG -resize ... -crop ... \) \
\( FOREGROUND.PNG -resize ... -crop ... \) \
-composite RESULT.JPG
Note that if you use Windows:
the backslashes at line-ends become carets (^) and may not have trailing spaces after them,
the backslashes preceding opening and closing parentheses must be omitted,
most single quotes probably need replacing with double quotes - probably not applicable to this specific question.
Note that your command is probably unnecessarily complicated, I would recommend omitting quite a lot of it and seeing how you get on. For example, all the define png:XXX=YYY settings are irrelevant if you aren't creating a PNG as output.
You can carry forward the JPEG-related parameters (quality, interlace, upsampling) and put them in anywhere you like, probably at the start like this, but put the colorspace and strip at the end to ensure both input files are stripped:
magick -quality 82 -define jpeg:fancy-upsampling=off \
BACKGROUND.PNG ... \
FOREGROUND.PNG ... \
-composite -colorspace sRGB -strip RESULT.JPG

Related

How to pass Imagemagick montage output and image properties to the convert command without using any temporary files?

I have a set of images, and I can use the Imagemagick montage command on them to produce a montage image file with transparent background (let's call this fgimg). Now I have another existing image (let's call this bgimg) that I'd like to use (after some special processing with the convert command) as the background for fgimg, which can be achieved within the same convert command. At this point it seems trivial to avoid writing the temporary fgimg to disk, simply by piping the standard output of montage to the standard input of convert.
My problem is that the special processing I'm applying to bgimg will require some knowledge of the image properties of fgimg (e.g., resizing bgimg to have the same size as fgimg), which I don't know in advance. How can this information be retrieved and used in the convert command?
Note: I'm using Imagemagick version 6.9.7-4 on Linux.
I'll include some commands below to further illustrate the problem in detail.
The following command produces the montage image fgimg from a set of input images. The output is in the 'special' miff format (which seems best for temporary output to be worked on later), and has transparent background so that the actual background can be applied later. Most of the other options here are not important, but the point is that the output size (dimensions) cannot be determined in advance.
montage input_*.jpg -tile 5x -border 2 -geometry '200x200>+20+20' \
-gravity center -set label '%f\n%G' -background none -fill white \
-title 'Sample Title' miff:fgimg
Next, I have another input image bgimg.jpg. I want to perform some processing on it before using it as background to fgimg. The processing can be quite complex in general, but in this example, I want to:
resize bgimg.jpg to fit inside the dimensions of fgimg without any cropping;
apply a fade-to-black effect around the edges;
make it the same size as fgimg, with a black background;
combine this with fgimg to produce the final output.
Notice that I need the size of fgimg in two places. I can first extract this into a shell variable:
size=$(identify -format '%G' miff:fgimg)
Then I can do all the steps above in one convert command (note that $size is used twice):
convert "bgimg.jpg[$size]" -gravity center \
\( +clone -fill white -colorize 100% -bordercolor black \
-shave 20 -border 20 -blur 0x20 \) -compose multiply -composite \
-background black -compose copy -extent $size \
miff:fgimg -compose over -composite final_out.jpg
Now here is the problem: I want to avoid writing the temporary file fgimg to disk.
I could replace miff:fgimg with miff:- in both the montage and convert commands and then just pipe one to the other: montage ... | convert .... But how do I deal with the $size?
I tried to use file descriptors (miff:fd:3) but this does not seem to work, which is confirmed by the comments to this question.
Is there a way to do this (in Imagemagick v6) without creating a temporary file?
This example command uses ImageMagick v6 on a bash shell. Instead of "montage" it starts by using "convert" to create a "logo:", one of IM's built-in sample images, then pipes it out as a MIFF and into the "convert" command that follows. You can pipe the output of "montage" just as easily. And it uses another IM built-in image "rose:" as your "bgimg.jpg"...
convert logo: miff:- | convert - rose: \
+distort SRT "%[fx:t?min(u.w/v.w,u.h/v.h):1] 0" \
-shave 1 +repage -gravity center -bordercolor black \
\( -clone 1 -fill white -colorize 100 -shave 6 -border 6 \
-blur 0x6 -clone 1 -compose multiply -composite \) -delete 1 \
\( -clone 0 -alpha off -fill black -colorize 100 \
-clone 1 -compose over -composite \) -delete 1 \
+swap -composite final_out.jpg
That reads the piped image "-" and the background image "rose:".
Then it uses "+distort" with an FX expression to scale "rose:" to the maximum dimensions that still fit within the original piped input image. That operation adds a pixel all around so we use "-shave 1" to get rid of that.
Next inside parentheses it clones that re-scaled background image, makes an edge blur mask, and composites them to make the fade-to-black edge on the background image. Right after the parentheses it deletes the non-edged background image.
In the next parentheses it clones the input image, makes it black, clones the modified background image, and composites it centered over the black one. Again the non-extended background image is discarded after the parentheses with "-delete 1".
Finally the modified background and the input image are put in the proper order with "+swap" and composited for the final output. Run this command without the last "-composite" to see the two images that result from the prior parts of the command.
Both the main input image and background image can be any size, any dimensions, and any aspect ratio. This works for me on v6.8.9 on a bash shell. It should work on any ImageMagick newer. It should work on Windows by removing all the backslashes that escape parentheses and changing the continued-line backslashes "\" to carets "^".
EDITED TO ADD:
You can use that FX expression to find the scaling amount, save it as a variable, then isolate the background image inside parentheses and use that variable to do the scaling and shaving there. That way it only affects the background image. There may be a rounding error with that, but the main image, which determines the exact final output dimensions, will be unaffected. Note the difference in the first few lines of this command...
convert logo: miff:- | convert - rose: \
-set option:v1 "%[fx:min(u.w/v.w,u.h/v.h)]" \
\( -clone 1 +distort SRT "%[v1] 0" -shave 1 \) -delete 1 \
+repage -gravity center -bordercolor black \
\( -clone 1 -fill white -colorize 100 -shave 6 -border 6 \
-blur 0x6 -clone 1 -compose multiply -composite \) -delete 1 \
\( -clone 0 -alpha off -fill black -colorize 100 \
-clone 1 -compose over -composite \) -delete 1 \
+swap final_out.jpg

Merge two Imagemagick convert commands

I'm trying to Create a thumbnail from GIF 1st command and then merge another image on top of it 2nd command.
1st command:-
convert -thumbnail 398x398 -auto-orient -quality 85 giphy.gif[0] output.jpg
2nd command:- convert -size 1920x1080 xc:none output.jpg -blur 5x4 -composite out1.png -gravity center -composite outfinal.png
I've tried using like this but didn't work out:
convert -thumbnail 398x398 -auto-orient -quality 85 giphy.gif[0] output.jpg | convert -size 398x398 xc:none output.jpg -blur 5x4 -composite out1.png -gravity center -composite outfinal.png
Image extracted from gif:-
2nd image:
Final output as merge two images :
Thank you:)
This is a pretty easy way to do it:
convert -gravity center background.jpg -blur 5x4 \
\( yinyang.png -resize 130x78\! \) \
-composite result.jpg
I put a spurious -resize 130x78\! in there so you can see where to do extra operations that only affect the little "yin yang" picture in the middle - you can obviously remove it.
Maybe I can explain the logic... the first line of the command deals with the background, the second line deals with the overlay. So, first, load up the background picture and make all the edits you want to it, i.e. blurring. Then start some "aside-processing" in parentheses which loads up the "yin yang" and applies some edits to that exclusively without affecting the background image. Then, when you are happy with the overlay, exit the parentheses and take the result of the "aside-processing" and overlay it on top of the background. It overlays into the centre because I set the gravity beforehand.
Hope that helps.
Added in response to your comment... if you want to do the extraction as well, just do that in the "aside-processing":
convert -gravity center \
giphy.gif[0] -thumbnail 398x398 -auto-orient -blur 5x4 \
\( yin yang -resize 300x100\! \) \
-composite result.jpg

Assembling an image from parts of others with ImageMagick (image crop syntax)

I have a set of source PNG images and I want to use parts of them to assemble a final PNG image. The parts are rectangular and never overlap on the destination but are of different sizes. Sometimes it is the whole of a source image and sometimes just a subsection. I want to edit the sources many times and re-assemble the final image each time, so I tried to write a script using sh and Imagemagick to do it.
I tried this
convert \
-size 512x512 null:\
-page +96+32 source_a.png\
-page +96+0 source_b.png[32x32+16+16] \
-background transparent\
-layers merge\
destination.png
(just with two source images for illustration)
I want all of source_a.png and a piece of source_b.png. The first is OK, but using the 'inline crop' syntax on source_b.png gives me an error:
convert: geometry does not contain image `source_b.png' # warning/transform.c/CropImage/666.
The image is big enough:
$ identify source_b.png
source_b.png PNG 64x48 64x48+0+0 8-bit sRGB 3.7KB 0.000u 0:00.000
What's the best way to do this? I am using ImageMagick 6.9.7-0 Q16 on MacOS 10.12
An alternative might be to use -geometry and -composte to achieve the same effect:
convert -size 512x512 xc:white \
source_a.png -geometry +96+32 -composite \
source_b.png[32x32+16+16] -geometry +96+0 -composite \
result.png
PNG's will preserve the paging from inline cropping, so the addition page will through the ROI out of bounds. I imaging it'll be simpler to -repage the inline crop then attempting to clear previous paging & setting new page.
convert -size 512x512 null: \
-page +96+32 source_a.png \
\( source_b.png[32x32+16+16] -repage +96+0 \) \
-background transparent\
-layers merge\
destination.png

How to blend + translate simultaneously with Imagemagick (or some other scriptable software like Photoshop)?

How can I blend AND translate at the same time ?
Something like this : http://www.imagemagick.org/Usage/layers/#flatten but in such a way that the images are transparent.
I was trying :
composite -blend 90 -page +0+0 input01.jpg -page +500+0 input02.jpg -resize x400 outputSimpleMosaicBlend01.
but this did not work.
So if I have two input images:
Then how can I get an image that looks like the composite image below ?
Any suggestions how to do this programatically (not manually) with ImageMagick ? Or some other tools ?
I would like to create several thousands of composite images like that (for an animation) and I would like to automate the process.
The problem is that I can find examples that overlay images and that translate images but I cannot find examples that do these two operations simultaneously.
This is the main goal of this question, to give such code/script examples, how to do that with image manipulation tools like ImageMagick programmatically.
EDIT:
Things that I tried and did not work:
convert a.jpg -geometry +100+0 b.jpg -compose blend -composite result.jpg
gives:
I tried
convert -background none a.jpg -geometry +100+0 b.jpg -compose blend -composite result.jpg
too which gives the same result.
I got this :
with this
convert -background none input01.jpg input02.jpg -geometry +1200+0 -compose blend -define compose:args=50 -composite result.jpg
command.
It's getting close ! Thanks Mark!
A slightly different way of doing this is to set the width of the output image using -extent and then to overlay the right hand image using -gravity East to align it to the right edge - seems a fraction more intuitive to me - but go with whatever works for you!
convert a.jpg -background white -extent 2800x \
\( b.jpg -resize 150% -alpha on -channel A -evaluate set 50% +channel \) \
-gravity east -composite result.jpg
Thanks to Snigbo, the following command :
convert input02.jpg \( input01.jpg -resize 150% -alpha Opaque -channel A -evaluate Multiply 0.5 +channel -set page +1200+30 \) -background White -layers merge a.jpg
produces:

Image magick to put images in row and then generate reflection

I was wondering if I can use imagemagick to produce a result image like this
My initial image will be like this
I am able to create the dark image to be put in the background like this -
convert -brightness-contrast -20x10 GARDENS-ILLUSTRATED_JAN-14.jpg out_lighter2.jpg
But I do not know how to put them in line like that and then generate a reflection.
I have got something close, but have no more time, so maybe you can fiddle with it and get it to what you want:
#!/bin/bash
input=input.png
# Calculate width and height
w=$(convert $input -ping -format "%w" info:)
h=$(convert $input -ping -format "%h" info:)
# Calculate how much to chop off bottom and height of reflection
chop=$(echo $h*60/100|bc)
refl=$(echo $h*20/100|bc)
convert $input -alpha on \
\( +clone -flip -size ${w}x${refl} gradient:gray40-black -alpha off -compose CopyOpacity -composite \) \
-append -background white -compose Over -flatten -gravity South -chop 0x${chop} output1.png
# Darken and reduce for second layer
convert output1.png -brightness-contrast -25x8 -resize 90% output2.png
# Darken and reduce for third layer
convert output2.png -brightness-contrast -25x8 -resize 90% output3.png
# Make big new output canvas
neww=$(echo $w*1.6|bc)
newh=$(echo $h*1.6|bc)
# Splat images onto it with offsets
convert -size ${neww}x${newh} xc:transparent -page +0+80 output3.png \
-page +40+40 output2.png \
-page +80+0 output1.png -flatten output.png
My basic idea is to generate the bright, frontmost image first in output1.png, then darken and reduce it into output2.png and again into output3.png. Then composite them all together into output.png with a little offset.

Resources