imagemagic: combine two grid images - image

I have two big images with identical dimensions.
Each image composed on the 4x6 elements splited in the form of grid (table) with identical border. Here is the both inputs in resized format (actual resolution is 7384x2872)
Is it possible to use any utilities of Image Magic or other software working on Mac, to take any number of the central grids (surrounded by a perimeter by other elements) from the first image and insert it to the same central position of the image 2 (thus making a substitution of selected grids )?

Let's call your first image a.jpg and the second b.jpg and assume we want to copy the right-most cell from the second row of the first into the second.
Now that cell is 112x64 and at offset +577+69 from the top-left corner, so we want to:
load the first image
crop out the cell
load the second image
put the second image at the bottom
flatten the cropped part of the first image onto the second
That looks like this:
magick a.jpg -crop 112x64+577+69 b.jpg +swap -flatten result.jpg
I have made an animation of the a.jpg, b.jpg and the result here:
If you dislike doing things in the "wrong" order, you can do exactly the same thing by loading b.jpg first, then in some bracketed "aside-processing" loading and cropping just a.jpg before flattening:
magick b.jpg \( a.jpg -crop 112x64+577+69 \) -flatten result.jpg

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.

Insetting smaller image inside a larger one using ImageMagick

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.

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

How to trim/crop an image uniformly in ImageMagick?

Assume I have an original image (gray backgorund) with a black circle a bit down to the right (not centered), and the minimum space from any edge of the circle to the edge is, lets say, 75px. I would like to trim the same amount of space on all sides, and the space should be the maximum space possible without cropping the actual object in the image (area in magenta in image). Would love to hear how this could be solved.
Thanks in advance!
If I understand the question correctly, you want to trim an image not based on minimum bounding rectangle, but outer bounding rectangle.
I would do something like this..
Given I create an image with.
convert -size 200x200 xc:gray75 -fill black -draw 'circle 125 125 150 125' base.png
I would drop the image to a binary edge & trim everything down to the minimum bounding rectangle.
convert base.png -canny 1x1 -trim mbr.png
This will generate mbr.png image which will also have the original page information. The page information can be extracted with identify utility to calculate the outer bounding rectangle.
sX=$(identify -format '%W-(0 %X)-%w\n' mbr.png | bc)
sY=$(identify -format '%H-(0 %Y)-%h\n' mbr.png | bc)
Finally apply the calculated result(s) with -shave back on the original image.
convert base.png -shave "${sX}x${sY}" out.png
I assume that you want to trim your image (or shave in ImageMagick terms) by minimal horizontal or vertical distance to edge. If so this can be done with this one liner:
convert circle.png -trim -set page "%[fx:page.width-min(page.width-page.x-w,page.height-page.y-h)*2]x%[fx:page.height-min(page.width-page.x-w,page.height-page.y-h)*2]+%[fx:page.x-min(page.width-page.x-w,page.height-page.y-h)]+%[fx:page.y-min(page.width-page.x-w,page.height-page.y-h)]" -background none -flatten output.png
This may look complicated but in reality isn't. First trim the image. The result will still have stored information on page geometry including original width, height and actual offsets. With this info I can set the page geometry (correct width & height and new offsets) using ImageMagick FX expressions. Finally, flattening the image will produce desired output.

Resources