Command for merging 2 image files

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
satimis
Posts: 30
Joined: 2017-04-13T19:20:03-07:00
Authentication code: 1151

Command for merging 2 image files

Post by satimis »

Hi all,

Instead of running GIMP is there a command to merge 2 images?

example:
image-1.png Text with transparent background
image-2.png For background

I expect the new page with the Text centered and image-2 as background. I have looked at;
Appending Images
http://imagemagick.org/Usage/layers/#append

and couldn't resolve which command to be used. Please help.

Thanks

Regards
satimis
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Command for merging 2 image files

Post by fmw42 »

Please always provide your IM version and platform, since syntax may vary.

The basic concept is -compose over -composite. See http://www.imagemagick.org/Usage/compose/#compose

For IM 6

Code: Select all

convert background image overlayimage -gravity center -compose over -composite resultimage


For IM 7

Code: Select all

magick background image overlayimage -gravity center -compose over -composite resultimage
If these do not work, please post your images to some free hosting service such as dropbox.com and put the URLs here.
satimis
Posts: 30
Joined: 2017-04-13T19:20:03-07:00
Authentication code: 1151

Re: Command for merging 2 image files

Post by satimis »

fmw42 wrote: 2017-04-13T19:42:20-07:00 Please always provide your IM version and platform, since syntax may vary.
Oh, sorry

⟫ apt-cache policy imagemagick

Code: Select all

imagemagick:
  Installed: 8:6.8.9.9-7ubuntu5.3
  Candidate: 8:6.8.9.9-7ubuntu5.3
  Version table:
 *** 8:6.8.9.9-7ubuntu5.3 100
        100 /var/lib/dpkg/status
     8:6.8.9.9-7 500
        500 http://ubuntu.01link.hk xenial/main amd64 Packages
 


⟫ convert -version

Code: Select all

Version: ImageMagick 6.8.9-9 Q16 x86_64 2016-11-29 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2014 ImageMagick Studio LLC
Features: DPC Modules OpenMP
Delegates: bzlib cairo djvu fftw fontconfig freetype jbig jng jpeg lcms lqr ltdl lzma openexr pangocairo png rsvg tiff wmf x xml zlib
Platform: Ubuntu 16.04
The basic concept is -compose over -composite. See http://www.imagemagick.org/Usage/compose/#compose

For IM 6

Code: Select all

convert background image overlayimage -gravity center -compose over -composite resultimage
For IM 7

Code: Select all

magick background image overlayimage -gravity center -compose over -composite resultimage
If these do not work, please post your images to some free hosting service such as dropbox.com and put the URLs here.
Thanks for your advice. Following command works for me;

Code: Select all

⟫ composite -blend 80 -gravity center chinese_08_transp.png scenery-008.jpg output_file.png
Pls see attached photo. (sorry I don't know how to upload the photo here)

If I expect the Text to be at center bottom what flag shall I use? Thanks

Regards
satimis

Image
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Command for merging 2 image files

Post by fmw42 »

You can post your images to some free hosting service such as dropbox.com and put the URL here.

You would be better off using convert .... -composite, rather than composite. The former is more flexible.

Depending upon how big your text image is relative to your background, use -gravity south -geometry +X+Y.

Once I see your images, I can give you a better command.
satimis
Posts: 30
Joined: 2017-04-13T19:20:03-07:00
Authentication code: 1151

Re: Command for merging 2 image files

Post by satimis »

fmw42 wrote: 2017-04-14T09:06:52-07:00 You can post your images to some free hosting service such as dropbox.com and put the URL here.

You would be better off using convert .... -composite, rather than composite. The former is more flexible.

Depending upon how big your text image is relative to your background, use -gravity south -geometry +X+Y.

Once I see your images, I can give you a better command.
Your advice noted. Thanks

Photo on Dropbox
https://www.dropbox.com/s/u5agltgofyd5s ... e.png?dl=0

Regards
satimis
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Command for merging 2 image files

Post by fmw42 »

I was also asking for your input images and/or definition of text.
satimis
Posts: 30
Joined: 2017-04-13T19:20:03-07:00
Authentication code: 1151

Re: Command for merging 2 image files

Post by satimis »

fmw42 wrote: 2017-04-14T18:25:24-07:00 I was also asking for your input images and/or definition of text.
Just upload other 2 files to Dropbox

Thanks

Regards
satimis
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Command for merging 2 image files

Post by fmw42 »

I only see your output file at the link above. Please post the URLs to the other files.
satimis
Posts: 30
Joined: 2017-04-13T19:20:03-07:00
Authentication code: 1151

Re: Command for merging 2 image files

Post by satimis »

fmw42 wrote: 2017-04-14T19:00:28-07:00 I only see your output file at the link above. Please post the URLs to the other files.
Oh sorry. Tried again

Input file - Text
https://www.dropbox.com/s/h7475qdtjqipw ... p.png?dl=0

Input file - background
https://www.dropbox.com/s/kyqqa4mcmg3pn ... 8.jpg?dl=0

Output file
https://www.dropbox.com/s/u5agltgofyd5s ... e.png?dl=0
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Command for merging 2 image files

Post by fmw42 »

This is the more current command for IM 6. If IM 7 change "convert" to "magick"

Code: Select all

convert scenery-008.jpg chinese_08_transp.png -gravity center -define compose:args=80 -compose blend -composite result.png
If you want the text offset from the center, then add -geometry +X+Y right after -gravity center, where X is the horizontal shift and Y is the vertical shift.

see
http://www.imagemagick.org/script/compose.php
http://www.imagemagick.org/Usage/layers/#convert

If you want to resize the text image, then use parenthesis processing such as

Unix syntax:

Code: Select all

convert scenery-008.jpg \( chinese_08_transp.png -resize X% \) -gravity center -define compose:args=80 -compose blend -composite result.png

Windows syntax:

Code: Select all

convert scenery-008.jpg ( chinese_08_transp.png -resize X% ) -gravity center -define compose:args=80 -compose blend -composite result.png
where X here is the resize percent. See http://www.imagemagick.org/script/comma ... php#resize and http://www.imagemagick.org/script/comma ... p#geometry
satimis
Posts: 30
Joined: 2017-04-13T19:20:03-07:00
Authentication code: 1151

Re: Command for merging 2 image files

Post by satimis »

fmw42 wrote: 2017-04-14T20:00:46-07:00 This is the more current command for IM 6. If IM 7 change "convert" to "magick"

Code: Select all

convert scenery-008.jpg chinese_08_transp.png -gravity center -define compose:args=80 -compose blend -composite result.png
If you want the text offset from the center, then add -geometry +X+Y right after -gravity center, where X is the horizontal shift and Y is the vertical shift.

see
http://www.imagemagick.org/script/compose.php
http://www.imagemagick.org/Usage/layers/#convert

If you want to resize the text image, then use parenthesis processing such as

Unix syntax:

Code: Select all

convert scenery-008.jpg \( chinese_08_transp.png -resize X% \) -gravity center -define compose:args=80 -compose blend -composite result.png

Windows syntax:

Code: Select all

convert scenery-008.jpg ( chinese_08_transp.png -resize X% ) -gravity center -define compose:args=80 -compose blend -composite result.png
where X here is the resize percent. See http://www.imagemagick.org/script/comma ... php#resize and http://www.imagemagick.org/script/comma ... p#geometry
Hi,

Performed following tests;

1)

Code: Select all

⟫ convert scenery-008.jpg chinese_08_transp.png -gravity center -define compose:args=80 -compose blend -composite result_01.png
result_01.png
https://www.dropbox.com/pri/get/result_ ... A20mp6S1zg

2)

Code: Select all

⟫ convert scenery-008.jpg \( chinese_08_transp.png -resize 150% \) -gravity center -define compose:args=80 -compose blend -composite result_02.png
result_02.png
https://www.dropbox.com/pri/get/result_ ... CgSJwvsutA

3)

Code: Select all

⟫ convert scenery-008.jpg chinese_08_transp.png -gravity center -geometry +0+400 -define compose:args=80 -compose blend -composite result_05.png
result_05.png
https://www.dropbox.com/pri/get/result_ ... lF8ET5s9DQ

4)

Code: Select all

⟫ convert scenery-008.jpg chinese_08_transp.png -gravity center -geometry +0-400 -define compose:args=80 -compose blend -composite result_06.png
result_06.png
https://www.dropbox.com/pri/get/result_ ... GP_B83WDOg

Referring to 3) and 4)
Why +0+400 (y=+400) moving the Text to bottom not to top?
Why +0-400 (y=-400) moving the Text to top not to bottom?


If I need ;
1) adding another image_02.jpg to the top right sector of the background.
2) the Text images is still in the center retaining its original size
3) image_02.jpg to be in a circle trimmed, size any such for testing

image_02.jpg
https://www.dropbox.com/pri/get/image_0 ... DOcU27Gfbg

What will be the command?

Or I need to do it with 2 commands (2 steps)?

Thanks

Regards
satimis
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Command for merging 2 image files

Post by fmw42 »

Sorry it appears that you have used a private directory at dropbox and I would need login/password access. But, you can make the links to these files shareable. Dropbox recently changed their public folder so that it is not shareable as I recall. So read the instructions for making a link shareable to a file.
satimis
Posts: 30
Joined: 2017-04-13T19:20:03-07:00
Authentication code: 1151

Re: Command for merging 2 image files

Post by satimis »

fmw42 wrote: 2017-04-16T10:54:09-07:00 Sorry it appears that you have used a private directory at dropbox and I would need login/password access. But, you can make the links to these files shareable. Dropbox recently changed their public folder so that it is not shareable as I recall. So read the instructions for making a link shareable to a file.
Oh, sorry,

Just recreated the links making them shareable.

result_01.png
https://www.dropbox.com/s/63pkyroqtwo9n ... 1.png?dl=0

result_02.png
https://www.dropbox.com/s/93fdjhtfshjvi ... 2.png?dl=0

result_05.png
https://www.dropbox.com/s/zi6s2fezdufdh ... 5.png?dl=0

result_06.png
https://www.dropbox.com/s/3p0sw9oiw9er5 ... 6.png?dl=0

image_02.jpg
https://www.dropbox.com/s/99q6efhcywznf ... 2.jpg?dl=0

Regards
satimis
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Command for merging 2 image files

Post by fmw42 »

Referring to 3) and 4)
Why +0+400 (y=+400) moving the Text to bottom not to top?
Why +0-400 (y=-400) moving the Text to top not to bottom?
In Imagemagick, rows increase downward (as positive). The 0,0 pixel is at the top left.

You can change the gravity setting to north if you want the text aligned at the top or northwest if you want the text aligned top left.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Command for merging 2 image files

Post by fmw42 »

If I need ;
1) adding another image_02.jpg to the top right sector of the background.
2) the Text images is still in the center retaining its original size
3) image_02.jpg to be in a circle trimmed, size any such for testing
Try this:

Code: Select all

dim=`convert image_02.jpg -format "%[fx:min(w,h)]\n" info:`
dim2=`convert xc: -format "%[fx:$dim/2]" info:`
convert scenery-008.jpg chinese_08_transp.png \
-gravity center -define compose:args=80 -compose blend -composite \
\( image_02.jpg -gravity center -crop ${dim}x${dim}+0+0 +repage -write mpr:img +delete \) \
\( mpr:img -fill black -colorize 100 -fill white -draw "circle $dim2,$dim2 0,$dim2" \
mpr:img +swap -alpha off -compose copy_opacity -composite \) \
-gravity northeast -compose over -composite \
result.png
In IM 7, this could be done in one command without separate commands for dim and dim2
Post Reply