Insert one image into another using

I have one image logo.pngsize 720x720, I want him to be stretched or compressed to fit the entire height and width, while maintaining the proportions centered within the limited rectangle top-left: 32,432, and bottom-right: 607,919another image background.pngsize image 640x960.

So, for the above example it logo.pngwill be changed to 488x488and will be placed in top-left: 76,432.

But I don’t want to calculate 488x488or 76,432, just want to use qualifiers top-leftand bottom-righthigher, that is, let ImageMagick understand this.

Can ImageMagick do something like this? If it cannot by itself, is there a solution for scenarios using convertand everything else?

+4
source share
1 answer

Hope an even simpler version

I think this still gives the same result, but easier:

#!/bin/bash

# Make initial images
convert -size 720x720! gradient:red-yellow -fill white -gravity center -pointsize 72 -annotate 0 "logo" logo.png
convert -size 640x960! gradient:blue-cyan  -fill white -gravity north  -pointsize 72 -annotate 0 "background" background.png

# Specify top-left and bottom-right
tl="32,432"
br="607,919"

# Get x1,y1,x2,y2 - bounding box of inserted image
IFS=, read -r x1 y1 <<< "$tl"
IFS=, read -r x2 y2 <<< "$br"

# Work out width and height 
w=$((x2-x1+1))
h=$((y2-y1+1))

# Resize logo proportionally, then extend canvas with invisible pixels to full size of insertion area and composite onto background
convert background.png \( logo.png -resize ${w}x${h} -background none -gravity center -extent ${w}x${h} \) -gravity northwest -geometry +${x1}+${y1} -composite result.png

Improved answer

It is simpler and faster, and we hope the same result:

#!/bin/bash

# Make initial images
convert -size 720x720! gradient:red-yellow -fill white -gravity center -pointsize 72 -annotate 0 "logo" logo.png
convert -size 640x960! gradient:blue-cyan  -fill white -gravity north  -pointsize 72 -annotate 0 "background" background.png

# Specify top-left and bottom-right
tl="32,432"
br="607,919"

# Get x1,y1,x2,y2 - bounding box of inserted image
IFS=, read -r x1 y1 <<< "$tl"
IFS=, read -r x2 y2 <<< "$br"

# Work out w and h, and smaller side "s"
w=$((x2-x1+1))
h=$((y2-y1+1))
s=$w
[ $h -lt $w ] && s=$h
echo Smaller side: $s

# Resize logo proportionally, then extend canvas with invisible pixels to full size of insertion area and place on background
convert background.png \( logo.png -resize ${s}x${s} -background none -gravity center -extent ${w}x${h} \) -gravity northwest -geometry +${x1}+${y1} -composite result.png

Original answer

I think from your comments that you want the resized image to center, so I did it.

In addition, there is a lot of debugging code, and I have not optimized it until I find out that I'm on the right track, so it can definitely be improved.

#!/bin/bash

# Make initial images
convert -size 720x720! gradient:red-yellow -fill white -gravity center -pointsize 72 -annotate 0 "logo" logo.png
convert -size 640x960! gradient:blue-cyan  -fill white -gravity north  -pointsize 72 -annotate 0 "background" background.png

# Specify top-left and bottom-right
tl="32,432"
br="607,919"

# Get x1,y1,x2,y2 - bounding box of inserted image
IFS=, read -r x1 y1 <<< "$tl"
IFS=, read -r x2 y2 <<< "$br"

# Work out w and h, and smaller side "s"
w=$((x2-x1+1))
h=$((y2-y1+1))
s=$w
[ $h -lt $w ] && s=$h
echo Smaller side: $s

# Work out size of resized image
read -r a b < <(convert logo.png -resize ${s}x${s} -format "%w %h" info:)
echo Resized logo: $a x $b

# Work out top-left "x" and "y"
x=$((x1+((w-a)/2)))
y=$((y1+((h-b)/2)))
echo x:$x, y:$y
convert background.png \( logo.png -resize ${s}x${s} +repage \) -geometry +${x}+${y} -composite result.png

enter image description here

enter image description here

enter image description here

+3
source

Source: https://habr.com/ru/post/1648477/


All Articles