Insetting smaller image inside a larger one using ImageMagick - bash

How would one go about insetting a smaller image inside a larger one?
I have two images as shown below:
Image 1:
Image 2:
The first image needs to go into the topleft corner of the second one. Its width is almost one tenth the width of the second one. I tried a number of things like compositing, and repage and merging layers, but I can't seem to get the hang of it yet. I am very new to imagemagick, so any help is appreciated.

Not certain what you mean exactly but this should give you an idea:
convert image1.png -bordercolor black -border 5 image2.png +swap -geometry +50+100 -composite result.png
I loaded the inset picture first and put a 5 pixel border around it, then loaded the background image, swapped them so the background was at the back and composited over the top.
I could, equally, have loaded the background image first, then loaded the inset image in some "aside-processing" and then composited the result on top:
convert image2.png \( image1.png -bordercolor black -border 5 \) -geometry +50+100 -composite result.png
I guess with the first method it looks kind of back-to-front and there is a +swap in there. With the second method, you have the "complexity" of the parentheses which make sure the border is only applied to the inset image and not the background image.

Related

ImageMagick: I need an option to mirror an image to the right of itself combined into one file

I need to take an image and append a mirror image of itself to the right (or left). It's got to do with mapping surfaces around spheres where some surface maps do not have left and right sides that "match up". (Digression: Is there is a name for the characteristic of an image whose right and left sides can be placed next to each other, wrapped around a cylinder or sphere, seamlessly?). Appending a mirror image to itself is a quick and dirty way to fix this problem in an environment where the image is chaotic and the viewer won't notice the trick being played.
This ImageMagick command almost does the trick but the problem is, it extends the image vertically as well as horizontally but leaves the vertical extent as an empty transparency. I just want a horizontal, mirror image extension:
convert original.jpg -background transparent -extent 200% \( +clone -flop \) -composite mirror.jpg
I need the -extent option to only extend in the horizontal direction. The ImageMagick documentation doesn't cover percentage values for -extent, only ratios like -extent 4:3 or absolute dimensions like -extent 800x600 but -extent 200% isn't in the documentation. Something like -extent 200% right is what I want. I have many images to run this transformation on and do not want to manually enter dimensions.

Merging two transparent gifs as layers

I've found this page on the imagemagick forum: Merging 2 gifs, one is animated and transparent which links to the imagemagick docs on Animation Modification; specifically, the example here:
convert canvas_prev.gif -coalesce \
-gravity NorthEast -draw 'image over 5,5 0,0 "rose:"' \
-layers Optimize draw_over.gif
Here's my attempt. I have these two gifs (i might be feeling a little morbid). The white in the first is actually transparent.
when I run
convert eyes.gif -coalesce -draw ' image over 0,0 0,0 "trump.gif" ' combine.gif
I get this:
which is not the complete effect I desire. The trump animation is no longer playing at all.
I want to see something more like this (this is created in Phaser JS, but this gives me no way to export the result as a new image other than manually recording a screencast):
One way to do it, yet not sure if it's the best, is as follows:
convert both animations into sprite-sheets of same dimensions
paste one image over the another
cut result into smaller frames and convert it into animated GIF
The commands should look like this:
montage -background none t.gif -tile x1# -geometry +0+0 tt.png
montage e.gif[0-16,0-9] -tile x1# -geometry +0+0 ee.png
magick convert -delay 10 -loop 0 ee.png tt.png -coalesce -flatten \
-crop 150x150 +repage output.gif
Tricky part is the second line with eyes image. It has only 17 frames while skull has 27. So the sprite sheet size must be adjusted.
I works, however I am not quite happy with this as solution requires manual entering of some parameters (frames selection and output image dimensions).

How can I stretch the edge colors of elements in an image with alpha channel?

I have an PNG with an alpha channel. First I want to find all elements in the png, then I want to stretch the last color on the border about 5 to 10 pixels out
The stretching has to be in all directions from the center of each object on the image (like triangle, rectangle...)
It is important that I keep the color of each pixel on the edge
Is this possible? Goal is in the future to automate the process with ImageMagick.
I have had a play with distort and it is probably the way to go but needs more investigation. I was working on a smaller image and the code is basically a proof of concept.
I think edge is the key option and with this example I reduced the size of the resized image all round by 20px in the distort.
I do not know how you would get the angled edges though.I know you do not want to distort the original but it may give you some ideas as to how to get the result you want.
convert "1.png" -trim -matte -virtual-pixel edge -mattecolor none -interpolate Spline +distort BilinearForward "0,0 20,20 400,0 380,20 400,95 380,55 0,95 20,55" -trim +repage "result.png"
I am not quite sure what is going on at the edges of your image, but this should get you started...
First, I trim off any transparent edges (with -trim) so we get to the actual pattern you seek to extend. Then clone the image and dispose of everything except the top row of pixels (with -crop). Then scale that row up till it is 800 pixels tall. Then exchange the tall top row with the original image in the processing order (using +swap) and append the original image below the height-extended top row.
convert cells.png -trim +repage \( +clone -crop x1+0+0 -scale x800! \) +swap -append result.png

Imagemagic: How to create one image on another image with first image as background

I have two image files. I have a image where text is written on a white background. The second image is a prescription and i want to merge the second image on first image with first image set as background.
First Image
Second Image
When i use below command i get below image
composite -geometry +100+20 firstImg.jpg secondImg.jpg finalImg.jpg
finalImage
I want the text in the second image to be merged to the first image. I am new to image magic and is stuck with this. Thanks in advance.
Something like this:
convert prescription.jpg \
\( words.jpg -resize 300x -fuzz 10% -transparent white \) \
-gravity center -geometry +0+80 -composite result.jpg
First I load the prescription template as a background, then I load and resize the words to the correct width, and then make the white (+/- 10%) areas transparent and overlay that onto the middle (-gravity center) with a small geometry offset to move it down little.
I hope you are not being naughty.
Use -compose Multiply to drop white from second image.
composite -geometry +100+20 firstImg.jpg secondImg.jpg -compose Multiply finalImg.jpg

Size variation in a Imagemagick montage

I try to have random space between my images in an Imagemagick montage.
My original script is:
montage images/* -mode Concatenate -tile 12x -geometry +10+10 -background white tile.png
…and I need something like:
montage images/* -mode Concatenate -tile 12x -geometry +($RANDOM%200+0)+($RANDOM%200+0) -background white tile.png
Is this possible?
The -geometry is only evaluated once at the start, so I don't think your intended approach is going to work. You can either grab the 12 images and total up their widths plus 11 randomly sized gaps and then create a canvas big enough to lay your images out on, or make a bunch of spacer blocks to move your images right and down by random amounts.
So, this is a rough starting point of how you might build something up maybe... I know you don't want the background yellow, but I am setting it that way so you can see what I am doing. I am basically laying out a red, a green and a blue image in a horizontal strip, but adding randomly sized spacer blocks in and I have coloured these in varying shades of grey so you can see them.
convert -background yellow \
red.png xc:gray20[100x100] \
\( xc:gray40[80x80] green.png -append \) \
xc:gray60[25x25] \
\( xc:gray80[200x200] blue.png -append \) \
+append tile.png
Each time an image and a spacer appear together in parentheses, that is basically a spacer ABOVE the image with the image itself -appended BELOW. Each time a spacer occurs on its own, that is basically to the right of the preceding image, and at the end, the +append joins all the blocks up side-by-side.
To understand what I am doing, you need to appreciate that -append places images top-to-bottom, one above the other and that +append places images side-by-side next to each other.
I am suggesting you create a script to build the command up.
Basically, you could make a strip like this, then another one of 12 and use -append to stick them together. Obvioulsy you want to make the background, and all the spacers, white.

Resources