add image to transparent section - imagemagick

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.

Let's say this 500x400 image is your starting image and it has transparent holes in it at 10,10 and 250,250.
Now, let's say you have two Mr Beans, bean1.jpg and bean2.jpg like this:
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
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
On Windows, you have to change the backslashes into carets, so \( becomes ^( and \) becomes ^).

Related

How to handle multiple elements with imagemagick cli?

We need to put 'information bar's to thousands of image files. For like a week or so i'm trying to learn imagemagick but i just couldn't figure this many elements out so i wanted to ask for a help here.
I get the idea of '-/+append'ing elements and swapping between them but when it comes to 3x3 matrix cells and text/image mixings, i just can't do it. As an example, i can get the 3 rows appended and a column next to it but i can't get to the next step of 'appending 2 more rows together then put them as a column block again' because when i try, all those append gets right or bottom as a whole image.. Well, you will get the idea when you see my brief image below..
magick.exe -size 150x100 -gravity center caption:"txt2" caption:"txt3" caption:"txt4" \
-append -size 94x294 xc:white -border 3 -swap 0,1 \
+append outoutout.jpg
FYI, height/width of rows/columns are there just for example not important.. And here comes MSPaint skills:
You have to create each section separately using parenthesis processing. Then if you want append them appropriately. Alternately, you can create a background image and compose ... -composite each image into its correct location.
Here is an example in ImageMagick using the second method.
Unix Syntax:
magick -size 400x400 xc:white \
\( barn.jpg -crop 400x200+0+0 +repage \) \
-geometry +0+0 -compose over -composite \
\( -size 100x200 xc:white -shave 5x5 -bordercolor black -border 5 \) \
-gravity northwest -geometry +0+200 -compose over -composite \
\( -size 100x200 -background white -gravity center -fill black \
-font Candice label:"Text1\n\nText2\n\nText3" \) \
-gravity northwest -geometry +100+200 -compose over -composite \
\( -size 100x200 -background skyblue -gravity center -fill red \
-font Arial label:"First_line\n\n\nSecond_line" \) \
-gravity northwest -geometry +300+200 -compose over -composite \
result.png
See for example:
parenthesis processing
appending
convert ... -composite

ImageMagick apply watermark multiple times

Currently, with ImageMagick I apply watermark.png on top of picture.jpg in the center using:
convert -size 700x1300 -composite picture.jpg watermark.png -gravity center -geometry 700x1300 output.jpg
This nicely overlays my watermark in the middle of the image. However, I want to achieve this effect in the top-left, center, and bottom-right. I've tried combining multiple -composites with no result. I would like to avoid calling 3 different commands to avoid the extra overhead it introduces, but I'm starting to believe it isn't possible without doing so.
What can I do to achieve the three watermark positions in one command, NorthWest, Center and SouthEast?
Hold the watermark image in a memory register with "-write mpr:watermark", then recall it into your command for the second and third composite. Here's a simple example of how that works...
convert watermark.png -write mpr:watermark input.png +swap \
-gravity center -composite mpr:watermark -gravity northwest -composite \
mpr:watermark -gravity southeast -composite output.png
Set the geometry before each composite if necessary to give the mark some padding from the corners.
In Imagemagick, there is a -gravity setting that allows you to put the watermark in various locations specified by the compass directions. See http://www.imagemagick.org/script/command-line-options.php#gravity. However, your syntax is not proper and may fail for use with IM 7. The proper syntax is to read the input image first. So your command should be
convert background watermark -gravity center -compose over -composite output
Change center to one of the other gravity settings such as northwest or southeast
Also your -size does nothing in your command. If you want to use -size WxH xc:somecolor, then you would use that in place of the background.
If you want to resize, then do not use -geometry. That is typically used for offsetting the gravity placement. Use -resize WxH if you want to resize the result.
See
http://www.imagemagick.org/Usage/compose/#compose
http://www.imagemagick.org/Usage/layers/#convert
I would modify GeeMack's answer just slightly so it reads a bit more symmetric:
convert watermark.png -write mpr:watermark +delete \
input.png \
mpr:watermark -gravity center -compose over -composite \
mpr:watermark -gravity northwest -compose over -composite \
mpr:watermark -gravity southeast -compose over -composite \
output.png
EDITED: to followup with OP question. If you need to resize the watermark, then do
convert watermark.png -resize 700x1300 -write mpr:watermark +delete \
input.png \
mpr:watermark -gravity center -compose over -composite \
mpr:watermark -gravity northwest -compose over -composite \
mpr:watermark -gravity southeast -compose over -composite \
output.png

Strange effect using 'distort perspective' on multiple images using ImageMagick

I want to create a 3D effect on an image so it looks like a folder, using the +distort Perspective' commands from ImageMagick.
However, if I use the distort multiple times (on different layers) in ImageMagick the result doesn't look correct.
Please see the attached image, left is correct, but the top-right corner of the right image looks strange.
I have used the following commands:
convert square.jpg -brightness-contrast 0x0 -resize 700x700 \
\( -clone 0 -flip -crop 700x140+0-0 -size 700x140 gradient:gray60-black -compose CopyOpacity -composite \) \
-append front.png
convert square.jpg -brightness-contrast -50x0 -resize 700x700 \
\( -clone 0 -flip -crop 700x140+0-0 -size 700x140 gradient:gray60-black -compose CopyOpacity -composite \) \
-append inner.png
convert -background None -virtual-pixel transparent -background white \
\( inner.png +distort Perspective '0,0 0,0 700,0 735,-35 700,700 735,665 0,700 0,700' \) \
\( front.png +distort Perspective '0,0 0,0 700,0 665,35 700,700 665,735 0,700 0,700' \) \
-layers merge +repage 3dimage.jpg
I'm using ImageMagick v6.8
I'm not sure if I understand the layers in ImageMagick correctly.
Can someone see what's wrong? Thanks!
I'm not sure if I understand the layers in ImageMagick correctly. Can someone see what's wrong?
You're doing fine. Just need to repeat the -virtual-pixel transparent on the sub-image for front.png (and perhaps the inner.png too).
convert -background None -virtual-pixel transparent -background white \
\( inner.png +distort Perspective '0,0 0,0 700,0 735,-35 700,700 735,665 0,700 0,700' \) \
\( front.png -virtual-pixel transparent +distort Perspective '0,0 0,0 700,0 665,35 700,700 665,735 0,700 0,700' \) \
-layers merge +repage 3dimage.jpg

ImageMagick Create Image with background and paste 2 images which are resized

I create an Image with necessary size and background
convert -size 1000x750 xc:'#f4f5f6' /test.jpg 2>&1
I need to put other resized image to this new image and then one more for shadows
How to make it in one command?
The operators -geometry & -composite will handle this.
For example...
convert -size 500x375 xc:'#f4f5f6' \
rose: -geometry +100+100 -composite \
\( rose: -negate \) -geometry +400+200 -composite \
out.png

Combine and resize multiple images in a square using ImageMagick

So I want to create one large image of size 3600x2280 composed of three smaller images. The first should be resized to 1680x1050 and placed in the top left corner. The 2nd needs to be reiszed to 1920x1200 and placed immediately to the right of it (+1680 over). The 3rd image should be resized to 1920x1080 and placed on the bottom right (+1680+1200). The bottom left will just be blank/transparent.
I've tried various commands I've searched for online and think I'm getting somewhat close with something like the following for just two of the three images:
convert -define png:size=3600x2280 \( Photos/DSC05525-original.jpg -resize 1680x1050 \) -geometry +0+0 -composite \( Photos/Sydney-Loftus-Train-Station-original.jpg -resize 1920x1200 \) -geometry +1680+0 -extent 3600x2280 test.png
...but that places the 2nd image over the first (I think because it doesn't know to extend until the very end?). I've tried various combinations of -composite, -gravity and +repage, but can't seem to find a solution.
There are lots of ways of doing this. Choose the one that corresponds best to the way your mind works! I used test images like this:
1.jpg => red
2.jpg => green (lime actually)
3.jpg => blue
Method 1
convert -background none \
1.jpg -resize 1680x1050! \
\( 2.jpg -resize 1920x1200! \) +append \
\( 3.jpg -resize 1920x1080! -gravity east \) -append \
result.png
That says... "Leave all unpainted areas transparent. Resize image 1. Resize image 2 and place it to the right of image 1 (+append). Resize image 3 and align it East. Append that underneath images 1 and 2 (-append)."
Method 2
convert -background none \
\( 2.jpg -resize 1920x1200! \) \
\( 3.jpg -resize 1920x1080! \) -append \
\( 1.jpg -resize 1680x1050! \) +swap +append result.png
That says... "Load and resize image 2. Load and resize image 3. Place image 3 underneath image 2 (-append). Load and resize image 1. Place image 1 before (+swap) image 2 in the image list. Now append the second image in the list to the right of the first (+append)."
Method 3
convert -background none \
1.jpg -resize 1680x1050! -extent 3600x2280 \
\( 2.jpg -resize 1920x1200! -geometry +1680 \) -composite \
\( 3.jpg -resize 1920x1080! -geometry +1680+1200 \) -composite result.png
That says... "Leave any unpainted areas transparent. Load image 1 resize it then extend the canvas to the full output size to accommodate subsequent images. Load image 2, resize, position and splat onto canvas. Load image 3, resize and splat onto canvas."
Method 4
Just for fun, here's a totally different way of thinking about it:
{ convert 1.jpg -resize 1680x1050! miff:- ; \
convert 2.jpg -resize 1920x1200! miff:- ; \
convert -size 1680x1 xc:none miff:- ; \
convert 3.jpg -resize 1920x1080! miff:- ; } |
montage -background none -geometry +0+0 -tile 2x2 miff:- result.png
That says... "Start a compound statement that will load and resize 4 images and send each of them as a MIFF (Magick Image File Format) to a montage command that will put them all together in 2x2 grid (-tile 2x2) with no spaces between them (-geometry +0+0)."

Resources