Search code examples
image-processingimagemagickimagick

imagemagick overlay 2 images and keep the same name


I'm using magick to add png images to a t-shirt, and the images are saved as result01, result02, etc.. I was wondering if i could save the images with the names of the pngs from the folder "overlays" This is the code i'm using at the moment:

magick main.jpg -colorspace sRGB -gravity center null: ( ..\Pictures\overlays\*.png -resize 600x600 ) -geometry -10-30 -compose over -layers composite result%04d.png

I have also tried this, but it gives the name of the main image, not the overlay pngs: 

magick main.jpg -colorspace sRGB -gravity center null: ( ..\Pictures\overlays\*.png -resize 600x600 ) -geometry -10-80 -compose over -layers composite -set filename:f "%t_%p" %[filename:f].png

Thanks to everyone in advance


Solution

  • ImageMagick seems to hold the input file name of the bottom layer, the destination image of a composite. Here is one way to use the overlay images as the destination image, so those file names will be used as the "filename:0" variable, and still composite those overlays on top of the main image.

    (Note: The command is in Windows CMD format. For a BAT script you'd need to double all the percent signs "%%".)

    magick main.jpg -colorspace sRGB -background none -gravity center ^
       null: ( ..\Pictures\overlays\*.png -resize 600x600 ) ^
       -extent %[fx:u.w]x%[fx:u.h]-%[fx:t?10:0]-%[fx:t?80:0] ^
       -set filename:0 "%[t]_%[p]" -reverse -compose dstover ^
       -layers composite %[filename:0].png
    

    That reads in the main image and sets the colorspace, gravity, and background color. Then it adds the required "null:" image for the "-layers composite". Inside the parentheses it reads in all the overlay images and resizes them as needed.

    Next it uses an "-extent" operation that leaves the main image its original dimensions, and adds a transparent background canvas with the main image's dimensions to all the overlays, and locates the overlays with your "-10-80" geometry.

    Then set all the "filename:0" variables, and "-reverse" the order of the images. That will arrange the overlays as the destination images.

    Using the compose method of "dstover", the destination images, those overlays, will be composited on top of the main image, while using each overlay image's file name in the output image file name.