Search code examples
bashimagemagickmagick

magick: resize image before put it to a frame


I am using magick to add frames around my pics.

I have a frame picture where I keep transparent the picture placeholder area inside the frame:

enter image description here

Then I wrote a simple bash script that loops on the files in my pic directory and generates the framed pics.

WORKSPACE_HOME="..."
INPUT_IMG_HOME="$WORKSPACE_HOME/input/original-pics"
FRAME_IMG="$WORKSPACE_HOME/input/dia-frame/transparent-dia-1.png"
OUTPUT_IMG_HOME="$WORKSPACE_HOME/output/with-frame"

rm -f "$OUTPUT_IMG_HOME"/*
INDEX=0
for INPUT_IMG in $INPUT_IMG_HOME/*; do
    if [ -f "$INPUT_IMG" ]; then
        INDEX=$(expr $INDEX + 1)
        INPUT_IMG_FILENAME=$(basename $INPUT_IMG)
        INPUT_IMG_NAME=${INPUT_IMG_FILENAME%.*}
        OUTPUT_IMG="$OUTPUT_IMG_HOME/$(printf '%05d' $INDEX)-$INPUT_IMG_NAME.png"
        printf "($INDEX) framing \"${INPUT_IMG}\" to \"$OUTPUT_IMG\"...\n"
        $WORKSPACE_HOME/bin/magick "$INPUT_IMG" "$FRAME_IMG" +swap -auto-orient -gravity center -compose DstOver -composite "$OUTPUT_IMG"
    fi
done

Everything works great, but unfortunately the size of my pics do not fit properly to the size of the frame (dia). My pics are sometimes bigger, sometimes smaller then the frame's inside area. This is more critical when the pic has portrait orientation or smaller then the frame. For example:

portrait orientation, original image must be reduced: enter image description here

original image size must be increased:

enter image description here

Is that possible to instruct magick to resize the pics before put them to the frame?


Solution

  • I can't run your script itself, but barring aspect ratio weirdness, it seems to me you could just -resize.

        $WORKSPACE_HOME/bin/magick "$INPUT_IMG" -resize 2720x1550 "$FRAME_IMG" +swap -auto-orient -gravity center -compose DstOver -composite "$OUTPUT_IMG"
    

    As you probably know, options are processed serially. So by resizing the image before your frame gets mentioned, you effectively change just that first input file without affecting the frame or the result.

    The numbers I picked are approximately the space in your frame's transparent area, which is handily centered. Adjust to suit. If you tend to have wider images and would like to crop the sides to fill things vertically, just drop the first number, so:

    -resize x1550
    

    Is this the effect you're looking for?