0

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!

1 Answer 1

3

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
    
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you very much!. It works like a charm. Just need to ask how could i implement for batch images.
What OS do you use please?
I'm currently using Windows 10
Yikes! Can you use bash under WSL, or Cygwin? Can you use Powershell? Can you use CMD.EXE?
Cmd would be good.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.