Reputation: 357
INTRODUCTION
Let's say I have this image:
To the image I apply the mask in https://image.ibb.co/c5Nw6c/Mask.png (it's white, that's why I set it here). The mask is basically a message bubble with an arrow in the bottom right part.
The command (in bash) is the following:
size=`convert Mask.png -format "%wx%h" info:`
convert \( couple.jpg -channel rgba -alpha on -resize $size \) Mask.png -compose copy_opacity -composite -compose over -background transparent -flatten png:result.png
And the result is:
PROBLEM
Let's now check the next two images:
Image 2
Image 3
If we do the same with these two, we will obtain the following results:
What we see here is that in order for the mask to be applied correctly, the proportions must be similar, otherwise, depending on the original image's size, it might cropped in the bottom, or in the right.
QUESTION
How can I avoid the cropping problem and:
Thank you very much for your answers.
Upvotes: 0
Views: 650
Reputation: 53164
You are getting those results since the mask and the image have two different aspect ratios and sizes. You should resize the mask to conform exactly to the dimensions of the image rather than the other way around that you have done. So for example below, I have modified your Imagemagick command as follows:
infile="image.jpg"
size=`convert $infile -format "%wx%h" info:`
convert "$infile" \( Mask.png -resize $size! \) -compose copy_opacity -composite -compose over -background transparent -flatten png:result.png
Note I add the ! to the -resize $size!, so that the mask is forced to fit the dimensions of the input.
EDIT: If you do not want to distort the mask, then the only way is to resize and pad the image to fill it to the size of the mask. Here I have changed the background from transparent to black to show the effect. You can change it back to transparent.
infile="image.jpg"
size=`convert mask.png -format "%wx%h" info:`
convert \
\( "$infile" -resize $size -background white -gravity center -extent $size \) \
Mask.png \
-compose copy_opacity -composite \
-compose over -background black -flatten \
png:result2.png
Upvotes: 1
Reputation: 5395
This command should take any input image then scale it to fill the mask. Next it composites the image centered over the mask with "srcin" to show just the part of the image where the mask is white. It finishes by flattening onto a white background. The output will be the dimensions of the mask.
convert Mask.png image.jpg \
+distort SRT "0,0 %[fx:t?max(u.w/v.w,u.h/v.h):1] 0 0,0" \
-shave 1x1 +repage -gravity center -compose srcin -composite \
-compose over -background white -flatten result.png
This should work with IM 6.8.9 through IM 6.9.9.
Upvotes: 3