add background image to multiple images

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
mentose457
Posts: 3
Joined: 2015-02-17T18:18:15-07:00
Authentication code: 6789

add background image to multiple images

Post by mentose457 »

Thanks in advance for the help. Ive been going nuts over this for a few hours.

What I want to do is add a background image to a bunch of images while preserving the file name of the image that gets the background. So I'd like to use mogrify.

The image I want to use as the background image is simply called 'bg.png'. Note that parts of the image are transparent and semi-transparent.

The images I want to add the background to have a transparent background. They are named ic_*.png. Again I'd like to preserve the name of these images.

All of the images are the same dimensions, 128x128.

There is a bunch of fantastic info on convert but not too much on mogrify.

The version of ImageMagick im on is 6.7.7-10 2014-03-06 Q16

Again, thanks for any help.

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

Re: add background image to multiple images

Post by fmw42 »

you will need to use alpha compositing via -draw with mogrify as described at http://www.imagemagick.org/Usage/basics ... fy_compose

You just need to find the correct compose method. See http://www.imagemagick.org/Usage/compose/#duff-porter

If you cannot figure it out, post one pair of images to dropbox.com and put the URLs here. Then we can try to help.

Note that IM 6.7.7.10 is rather old and right when IM was undergoing major changes. It is about 130 versions old. You should consider upgrading.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: add background image to multiple images

Post by snibgo »

"convert" can do the job in a single command, but the output filename will usually be wrong.

Code: Select all

convert ic_*.png null: bg.png -composite out_%06.png
Output files will be named out_000000.png, out_000001.png, etc.

We can use a bit of magic:

Code: Select all

convert *.png -set filename:myname %t null: red.tiff -layers composite %[filename:myname].jpg
Now the outputs will have the same name as the inputs, but jpg instead of png. If I used png outputs, they would overwrite the inputs.
snibgo's IM pages: im.snibgo.com
mentose457
Posts: 3
Joined: 2015-02-17T18:18:15-07:00
Authentication code: 6789

Re: add background image to multiple images

Post by mentose457 »

Thanks fmw42. I got it. this is what ended up with:

Code: Select all

mogrify -alpha Set -draw 'image Dst_Over 0,0 0,0 "bg.png"' *.png
I also found out some of my images were not the correct size so I did this as well:

Code: Select all

mogrify -gravity center -background transparent -extent 128x128  *.png
The annoying thing is I tried something like that the other day when attempting to modify the info from a tutorial on how to add a watermark to an image.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: add background image to multiple images

Post by fmw42 »

try putting them together as

Code: Select all

mogrify -gravity center -background transparent -extent 128x128 -alpha Set -draw 'image Dst_Over 0,0 0,0 "bg.png"' *.png
does that work?
mentose457
Posts: 3
Joined: 2015-02-17T18:18:15-07:00
Authentication code: 6789

Re: add background image to multiple images

Post by mentose457 »

fmw42 wrote:try putting them together as

Code: Select all

mogrify -gravity center -background transparent -extent 128x128 -alpha Set -draw 'image Dst_Over 0,0 0,0 "bg.png"' *.png
does that work?
Thanks that also worked.
Post Reply