Search code examples
imagemagickimagickimagemagick-convert

add image to transparent section


I have an image with a couple of transparent boxes. I need to insert some specific images in to the transparent boxes. I tried several convert commands but couldn't end up with a solution.

I am using Windows 10 and imagemagick is working on my CLI with no issues. Hope someone can point me into the right direction.


Solution

  • Let's say this 500x400 image is your starting image and it has transparent holes in it at 10,10 and 250,250.

    enter image description here

    Now, let's say you have two Mr Beans, bean1.jpg and bean2.jpg like this:

    enter image description here

    enter image description here

    Let's lay it out on a red background so you can see what is going on. We'll resize bean1.jpg and place him in the area of the top-left transparent hole, then we'll set up bean2.jpg for the bottom-right transparent hole:

    convert -size 500x400 xc:red  \
       \( bean1.jpg -resize 101x101! -geometry +10+10   \) -composite \
       \( bean2.jpg -resize 131x131! -geometry +250+250 \) -composite \  
       result.png
    

    enter image description here

    Now let's do that again, but this time, overlay the original image so the Beans peek through it:

    convert -size 500x400 xc:red  \
      \( bean1.jpg -resize 101x101! -geometry +10+10   \) -composite \
      \( bean2.jpg -resize 131x131! -geometry +250+250 \) -composite \  
     image.png -composite result.png
    

    enter image description here

    On Windows, you have to change the backslashes into carets, so \( becomes ^( and \) becomes ^).