How to add one image on top of another

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?".
Post Reply
suhaib
Posts: 3
Joined: 2017-03-06T02:26:29-07:00
Authentication code: 1151

How to add one image on top of another

Post by suhaib » 2017-03-06T02:34:47-07:00

i have two png files say the first one is bg.png and the second one is layer.png, now what i want to do is to place layer.png on bg.png using image magick, how can i do this?

User avatar
GeeMack
Posts: 607
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: How to add one image on top of another

Post by GeeMack » 2017-03-06T08:28:03-07:00

suhaib wrote:
2017-03-06T02:34:47-07:00
i have two png files say the first one is bg.png and the second one is layer.png, now what i want to do is to place layer.png on bg.png using image magick, how can i do this?
This is trivial with ImageMagick's "-composite" operator. The specific commands would depend on many factors like the desired location of the overlay on the background, whether any amount of transparency is required, how to manage any of the overlay that might fall outside the background, whether any resizing is necessary, and more.

It's also important for you to let us know which version of ImageMagick you're using and which OS or platform you're running it on, because the exact command can vary quite a bit depending on those criteria. Please read this post at the top of the category to help you form a proper question to get the most helpful answers.

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

Re: How to add one image on top of another

Post by fmw42 » 2017-03-06T11:08:17-07:00


suhaib
Posts: 3
Joined: 2017-03-06T02:26:29-07:00
Authentication code: 1151

Re: How to add one image on top of another

Post by suhaib » 2017-03-06T11:33:10-07:00

fmw42 wrote:
2017-03-06T11:08:17-07:00
For a start, see

http://www.imagemagick.org/Usage/compose/#compose (use the convert syntax, it is more flexible)
http://www.imagemagick.org/Usage/layers/#convert

Thanks for the quick response, I followed the steps and able to achieve desired results with the help of following command -

$ convert -gravity center a.png b.png -composite result.png

Now I want to run this in batch, I am using Ubuntu, what should I do to achieve this

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

Re: How to add one image on top of another

Post by fmw42 » 2017-03-06T11:47:49-07:00

Proper syntax reads the input first, then the settings, then the commands, then the output

Code: Select all

convert a.png  b.png -gravity center -composite result.png
Is the overlay image always the same? Is it in the same directory as your other images? Are all your background images in the same folder?

Please be more specific about what you want to do.

suhaib
Posts: 3
Joined: 2017-03-06T02:26:29-07:00
Authentication code: 1151

Re: How to add one image on top of another

Post by suhaib » 2017-03-06T12:02:53-07:00

fmw42 wrote:
2017-03-06T11:47:49-07:00
Proper syntax reads the input first, then the settings, then the commands, then the output

Code: Select all

convert a.png  b.png -gravity center -composite result.png
Is the overlay image always the same? Is it in the same directory as your other images? Are all your background images in the same folder?

Please be more specific about what you want to do.
No overlay image is not same, yes it is in the same directory, background image is in the same directory, actually the background will remain same while overlay images will change every time, I am using a jpg file as background, and png's in overlay. All files are in same directory. Using Ubuntu 16.10

User avatar
GeeMack
Posts: 607
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: How to add one image on top of another

Post by GeeMack » 2017-03-06T12:55:01-07:00

suhaib wrote:
2017-03-06T12:02:53-07:00
No overlay image is not same, yes it is in the same directory, background image is in the same directory, actually the background will remain same while overlay images will change every time, I am using a jpg file as background, and png's in overlay. All files are in same directory.
With most recent versions of IM6 you can use a single image as a background, overlay all the PNGs in the directory centered onto that background, and output the results as separate images with uniquely numbered file names, using a command like this...

Code: Select all

convert background.jpg null: *.png -gravity center -layers composite result%03d.png
That will output all the results to files named "result000.png", "result001.png", and so on.

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

Re: How to add one image on top of another

Post by fmw42 » 2017-03-06T15:54:12-07:00

If you need each image to be specially named, then you will need to write a script loop over each image in your directory, apply convert command to it and then write the output image. That is OS dependent.

Please, always provide your IM version and platform when asking questions, since syntax may differ. Also provide your exact command line and if possible your images.

See the top-most post in this forum "IMPORTANT: Please Read This FIRST Before Posting" at viewtopic.php?f=1&t=9620

For novices, see

viewtopic.php?f=1&t=9620
http://www.imagemagick.org/script/comma ... essing.php
http://www.imagemagick.org/Usage/reference.html
http://www.imagemagick.org/Usage/

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

Re: How to add one image on top of another

Post by fmw42 » 2017-03-06T16:02:53-07:00

Another way is to use mogrify with -draw to overlay the image rather than convert. Mogrify will process all images in the folder. I recommend your create a new directory and use the -path option to save your processed image.

cd path2/imagefolder
mogrify -path path2/newfolder -draw 'image over 0,0 0,0 "your_overlay_image.png"' *.jpg

see
http://www.imagemagick.org/Usage/basics/#mogrify
http://www.imagemagick.org/Usage/basics ... fy_compose

Post Reply