Using ImageMagick to Append One Image to Another

As described in another post, I wanted to combine two images to produce a third image. These two images were composites of multiple screenshots of a webpage. The composites were produced by Microsoft’s free Image Composite Editor (ICE). Each of them was narrow and long (i.e., high, vertically extensive), with dimensions like 500 pixels wide and 3000 (or even 30,000) pixels deep. There were actually a number of these images, but for simplicity I was just focusing on learning how to combine the first two — referred to here as A.png and B.png.

These images were too large to combine in Photoshop, and stitching programs like ICE did not seem to be able to figure out where the two images should be joined. (Later, as described in that other post, I would realize that there was a way to do this in ICE nevertheless; but I wasn’t sure the ICE approach would be as precise and, anyway, by then I had undertaken this exploration, and I wanted to preserve these notes for future reference.)

Summary

The solution, briefly, was to install ImageMagick with its default settings, and also with the option to install legacy commands; edit the system’s path so that the ImageMagick part would come first; use IrfanView to figure out the precise location where the upper left corner of the second file should begin, relative to the upper left corner of the first file; use an ImageMagick “convert” command to create a blank canvas big enough for both images; use an ImageMagick “composite” command to put the first of the two images where it needs to be on that canvas; and then use another “composite” command to add the second of the two images in the desired location.

Discussion

For concatenating two images, multiple comments in various online discussions pointed toward ImageMagick as the tool of choice. It seemed that I would probably want to use something like one of these ImageMagick commands, which I assumed would work once I had installed ImageMagick on my computer:

composite A.png B.png C.png
montage -mode concatenate -tile 1x in-*.jpg out.jpg
convert A.png B.png -append C.png

One comment suggested that the convert option would be better than the montage option (summary) if GhostScript fonts are not installed. It appeared the composite option would be useful if I intended to try to overlay two overlapping images, in the way that ICE and other photo stitchers did. I wanted to avoid, if possible, the complexity of trying to figure out by hand where my composite PNGs should overlap.

So I decided to start with the convert option. I ran the convert command suggested above. But it didn’t work: I got “Invalid Parameter – B.png.” A search led to the suggestion that it might be a PATH issue — that, specifically, Windows might be trying to use its own convert program first. The solution was to edit my path to put ImageMagick first. To do that, a search led to advice that I interpreted as follows: Control Panel > System > Advanced System Settings > Advanced tab > Environment Variables > System Variables > select Path > Edit > cut Variable Value > paste into Notepad > search for ImageMagick > cut and paste the ImageMagick entry to the start of the list, making sure the path entries potentially affected by this edit continue to be separated by a semicolon > paste the edited path back into the Variable Value box > OK.

That, by itself, did not make the command work. Officially, I was supposed to reboot the system to fully update the path change. The workaround was to kill and restart explorer.exe. (Note: this would close all open sessions of command window and Windows Explorer.) To do that, I ran taskmgr.exe > Processes tab > explorer.exe > right-click > End Process (repeat as needed to remove all instances of explorer.exe) > File > New Task > explorer.exe. But that still didn’t work; nor did rebooting fix it. Another source essentially advised looking for the ImageMagick installation folder (in my case, C:\Program Files\ImageMagick-7.0.4-Q16) and verifying that it did contain a convert.exe file. Mine didn’t. That would explain why the convert command did not run. In that case, the source suggested it might instead be the magick.exe file. Mine did have one of those. I modified the command accordingly:

magick A.png B.png -append C.png

That worked. C.png existed; it opened; and it contained A.png and B.png, albeit poorly joined — misaligned, that is, both horizontally and vertically.

In light of that misalignment, I was having second thoughts: maybe I should look into ImageMagick’s composite command after all. Unfortunately, composite.exe didn’t exist in the ImageMagick program folder either, and apparently there was also no Windows or other composite program — because when I ran the foregoing command, replacing “magick” with “composite,” I got an error: “‘composite’ is not recognized as an internal or external command, operable program or batch file.”

A search led to the insight that, when installing ImageMagick, I should have checked the option to “Install legacy utilities (e.g. convert).” I did that now — I just reinstalled over the top, not bothering to uninstall (and, incidentally, unchecking the box that said, “Add application directory to your system path,” since we had already taken care of that). I had to click Yes a dozen times to overwrite existing program files. Now the ImageMagick program folder had composite.exe and convert.exe. I tried again: “composite A.png B.png C.png” produced C.png. But C.png was not pleasing. It appeared to consist of A.png, shortened so that it had only the length of B.png.

The question I was developing seemed to be this: can ImageMagick move the second image up and to the right, so that it will overlap the bottom of the first image at a specified location? A search led to the “combine” command, which would apparently look something like this:

combine -geometry +100+150 A.png B.png C.png

where +100+150 apparently meant, “Put the upper left corner of B.png at a location 100 pixels higher and 150 pixels to the right.” Higher than what, and to the right of what, were questions whose answers were not immediately clear to me. I didn’t go too far with the combine option, because my searches kept leading back to convert, which was apparently the ImageMagick tool of choice. The desired procedure was apparently to use “convert” to create the canvas, and then use composite to position images within it, like this:

convert -size 1000x5000 C.png
composite -geometry +5+10 A.png C.png C.png
composite -gravity south -geometry 150x200+35+30 B.png C.png C.png

My understanding was that the first command would create the canvas, measuring 1000 pixels wide by 5000 pixels high, and would save it as C.png. The second command would combine C.png (i.e., the whole canvas) with A.png (starting 5 pixels above (?) and 10 pixels to the right of the upper left corner of C (?)), and would save this combination as a new version of C.png. (Note the advice not to use a lossy format, like JPG, for this sort of recurrent save-and-overwrite operation.) “Composite” would apparently work with only two files at a time. So then the third command would combine C.png (which now contained A.png within the original C.png canvas) with B.png. In this third command, B.png would be resized to 150×200 pixels and would be positioned 35 pixels above (?) and 30 pixels to the right (?) of (some position on) the bottom (i.e., south) edge of C (?) to produce an even newer generation of C.png.

Needless to say, there were a lot of unknowns here, for me, and the ImageMagick documentation I was finding was extremely opaque. Not to deny that it was doubtless very clear and simple for those who knew what they were doing. But I had spent a couple hours spinning my wheels just to get this far, reading various posts and explanations uncovered through multiple searches, and still not finding a straightforward answer to a seemingly simple question: how can I control the position B.png when I merge it with A.png?

It occurred to me that the discovery of the “gravity” option might give me what I needed to revise my previous use of “combine.” I tried this:

combine -gravity south -geometry +100+150 A.png B.png C.png

But, oops, combine.exe did not exist in the ImageMagick program folder. Well, OK, how about “composite”? I tried the same command with that one change. My translation of what it seemed to be doing: start at the bottom left corner of the image. From there, go 100 pixels to the right and 150 pixels up. (I was able to figure out pixel locations by viewing the image in IrfanView, clicking the mouse on the desired spot, dragging it a bit in some direction, and watching what happened to the information provided in the top bar of the IrfanView window.)

So anyway, that location, 100 pixels over and 150 pixels up, will be the bottom left corner of A. No geometry is specified for B, so start B right there at the bottom left corner. Now proceed upwards, with both A and B, until you reach the top edge of whichever one is shorter (in this case, B). That will be the top edge for both of them. Whatever might have appeared in the remainder of A, above that point, will be cropped.

That was what seemed to be happening with those commands. It wasn’t what I wanted. It appeared that I had better go back to the combination of several “convert” and “composite” commands shown above.

By using Windows Explorer > right-click > Properties > Details tab, I was able to determine the dimensions of A and B. Using that information, I devised a “convert” command big enough to accommodate A+B=C.png, and “composite” commands to position A and B within the C space as desired. The commands that worked for me:

convert -size 800x20000 xc:black C.png
composite A.png C.png C.png
composite -geometry +18+15766 B.png C.png C.png

Evidently I had to add something like the “xc” option (specifying a solid-color canvas) to give “convert” something to do; without it, the command produced an error (“convert: no images defined ‘C.png’ @ error/convert.c/ConvertImageCommand/3254”). With xc specified, I got a long, thin black image as C.png. Now, “composite A.png C.png C.png” gave me A.png, placed onto the black canvas of C.png, with both apparently starting at the upper left corner. That is, the excess black space of the original C canvas appeared at the right and at the bottom of this new C.png. (I chose black because that was the color of the canvas onto which ICE produced these PNG images. So I would hopefully wind up with my composite image, and everything outside the image, to the edge of the final PNG file, would be black.)

Alright. Now, how was I supposed to add B.png to this picture? I figured out where the upper left corner of B needed to be, within the C.png canvas. In fact, using IrfanView (draw with the mouse, Ctrl-Y to crop, then Ctrl-S to save), I cropped B.png to make it easier to figure that out. So the third of the three commands shown above says, run ImageMagick’s composite command with a geometry positioning the upper left corner of B.png at a point 18 pixels to the right and 15766 pixels down from the upper left corner of the old C.png, to produce the new C.png.

So that was my solution. That seemed to be how I could use ImageMagick to combine two images to create one larger (in this case, longer) image.

Advertisements
This entry was posted in Uncategorized and tagged , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s