1
votes

I apologize for the very specific set of circumstances, I don't know how helpful a solution would be to others. I'm looking for an automation solution for a image fetching and stitching task. I have images from a web source, that are stored in regularly incrementing folders, and with predictable file names. I would like to download these files, deposit them in folders based on their originating folders, and finally, stitch the files in each folder together in a simple grid.

Here are the specifics of the two steps.

Step 1

The images are stored in regular incrementing named folders in this format.

http://website.url/XX_[1-339][v/r]/XX/images.jpg

So the numbers will increment from 001 to 339 and each number will have a 'r' and 'v' version, so for example.

//XX_256r/XX/images.jpg or //XX_075v/XX/images.jpg

These URLs are otherwise always the same. Now within the folder themselves the contents are images broken into 131 256x256 .jpg's. These files are also highly regular and named according to a grid pattern, as [0-9]_[0-12].jpg, with the first image being 0_0.jpg and the last being 9_12.jpg.

I have been able to download these files folder by folder without difficulty by using a simple applescript. However it's still a bit involved to do this ~600 times. The next step is the part I have not been able to figure out even a one-off automated solution for.

Step 2

The images exist in their own folders, and the images themselves are a single larger images broken into 256x256 pixel sections. What I want to do next is simply assemble these images into a grid. The images are named for their position in the grid, so 0_0.jpg begins at the top left, and 1_0.jpg is the next position over. The grid is 9 spaces wide and 12 tall, so the first row of images will increment from 0_0.jpg to 9_0.jpg and the last row will be 0_12.jpg to 9_12.jpg. I'd like to automate the stitching of these images into a single image, so that it can be batch applied to the entire collection.

This is my goal. For this I have X questions:

  1. If I'm running OS X, can I perform the first step with a script in the Automator? I was able to fetch a single folder with a simple

    curl -O http://website.url/.../XX/[0-9]_[0-12].jpg
    

But I can't figure out how to create a script that will deposit images into folders based on their originating folder. So for example I can

curl -O http://website.url/XX_[001-339]v/XX/[0-9]_[0-12].jpg

Running it once with a v suffix and once with an r to fetch every images, however that will place a few hundred identically named files in the same folder. I'm not sure how to divide them up.

  1. Once I have all the images in their respectively named folders, how can I stitch them together? Since no image processing or modification needs to be done, this seems like it should be a feasible task through some kind of scripting, but as a total novice I've no idea where to start. 131 images have to be assembled into a JPG and this process needs to be repeated 680 times, so automation is desirable.

Any advice on where to begin would be greatly appreciated, I realize this is a pretty specific problem.

2

2 Answers

0
votes

Regarding your #1 question, the man page for curl has a "-o" option so you can specify where the saved files go. If you read that (shown below) you can see that it will work while fetching multiple documents using "#" while also using the "--create-dirs" option.

   -o, --output <file>
          Write output to <file> instead of stdout. If you are using {} or
          [] to fetch multiple documents, you can use '#'  followed  by  a
          number  in  the <file> specifier. That variable will be replaced
          with the current string for the URL being fetched. Like in:

            curl http://{one,two}.site.com -o "file_#1.txt"

          or use several variables like:

            curl http://{site,host}.host[1-5].com -o "#1_#2"

          You may use this option as many times as the number of URLs  you
          have.

          See  also  the --create-dirs option to create the local directo-
          ries dynamically. Specifying the output as '-' (a  single  dash)
          will force the output to be done to stdout.

Regarding question #2, see this link. See the answer by "Jonik" where he mentions the use of the montage command of ImageMagick.

And as I'm sure you're already aware, you can use the "do shell script" command from applescript to run both your curl and montage commands and create one applescript to automate all of this.

0
votes

I would do the curl and the image montage with shell script and ImageMagick, without bothering with Applescript.

If you don't know or have ImageMagick, I would suggest installing it with homebrew like this:

brew install imagemagick     

All you need to know is that +append will add the input images side-by-side with no gaps and that -append will add images below each other with no gaps.

#!/bin/bash
for r in {0..12}; do
  convert {0..9}_$r.jpg +append row$r.jpg
done
convert row{0..12}.jpg -append whole.jpg

So, if you save the above in a script called rebuild in your login directory ($HOME), and make it executable with

chmod +x $HOME/rebuild

Then all you need to do is go into a folder and type

$HOME/rebuild

The commands that the script will execute look like this fully expanded:

convert 0_0.jpg 1_0.jpg 2_0.jpg 3_0.jpg 4_0.jpg 5_0.jpg 6_0.jpg 7_0.jpg 8_0.jpg 9_0.jpg +append row0.jpg
convert 0_1.jpg 1_1.jpg 2_1.jpg 3_1.jpg 4_1.jpg 5_1.jpg 6_1.jpg 7_1.jpg 8_1.jpg 9_1.jpg +append row1.jpg
convert 0_2.jpg 1_2.jpg 2_2.jpg 3_2.jpg 4_2.jpg 5_2.jpg 6_2.jpg 7_2.jpg 8_2.jpg 9_2.jpg +append row2.jpg
convert 0_3.jpg 1_3.jpg 2_3.jpg 3_3.jpg 4_3.jpg 5_3.jpg 6_3.jpg 7_3.jpg 8_3.jpg 9_3.jpg +append row3.jpg
convert 0_4.jpg 1_4.jpg 2_4.jpg 3_4.jpg 4_4.jpg 5_4.jpg 6_4.jpg 7_4.jpg 8_4.jpg 9_4.jpg +append row4.jpg
convert 0_5.jpg 1_5.jpg 2_5.jpg 3_5.jpg 4_5.jpg 5_5.jpg 6_5.jpg 7_5.jpg 8_5.jpg 9_5.jpg +append row5.jpg
convert 0_6.jpg 1_6.jpg 2_6.jpg 3_6.jpg 4_6.jpg 5_6.jpg 6_6.jpg 7_6.jpg 8_6.jpg 9_6.jpg +append row6.jpg
convert 0_7.jpg 1_7.jpg 2_7.jpg 3_7.jpg 4_7.jpg 5_7.jpg 6_7.jpg 7_7.jpg 8_7.jpg 9_7.jpg +append row7.jpg
convert 0_8.jpg 1_8.jpg 2_8.jpg 3_8.jpg 4_8.jpg 5_8.jpg 6_8.jpg 7_8.jpg 8_8.jpg 9_8.jpg +append row8.jpg
convert 0_9.jpg 1_9.jpg 2_9.jpg 3_9.jpg 4_9.jpg 5_9.jpg 6_9.jpg 7_9.jpg 8_9.jpg 9_9.jpg +append row9.jpg
convert 0_10.jpg 1_10.jpg 2_10.jpg 3_10.jpg 4_10.jpg 5_10.jpg 6_10.jpg 7_10.jpg 8_10.jpg 9_10.jpg +append row10.jpg
convert 0_11.jpg 1_11.jpg 2_11.jpg 3_11.jpg 4_11.jpg 5_11.jpg 6_11.jpg 7_11.jpg 8_11.jpg 9_11.jpg +append row11.jpg
convert 0_12.jpg 1_12.jpg 2_12.jpg 3_12.jpg 4_12.jpg 5_12.jpg 6_12.jpg 7_12.jpg 8_12.jpg 9_12.jpg +append row12.jpg
convert row0.jpg row1.jpg row2.jpg row3.jpg row4.jpg row5.jpg row6.jpg row7.jpg row8.jpg row9.jpg row10.jpg row11.jpg row12.jpg -append whole.jpg