Page 1 of 1

Running multiple "mogrify" crop commands in sequence?

Posted: 2012-06-11T20:12:21-07:00
by featheredtar
Hi. I'm doing a project where I put multiple flowers on scanners and scan them five times an hour for 1-5 weeks until they are dried up. I then have image sequences composed of thousands of raw scans, from which I have to extract the individual flowers to bring into Adobe Premiere or After Effects. I'm trying to move from GUI to CLI-based processing, but can't find an answer to my problem. I've found out how to use the mogrify program to create crops of images within a directory and put them in another one. What I want to do though is define the crop parameters for, say, the 15 flowers that would be in one scanner image sequence, and then have IM run a script or something that would do those commands in sequence without my input. I could then define parameters for cropping each individual flower out of the raw scan, and then leave my computer for however long it would take for it to extract all the 15 flowers from the 1000s of raw images. This would be a big step up from the GUI based way, where I have to manually tell XnConvert to start a new crop batch each time one finishes, although I can process five crops at a time with multiple instances of XnConvert. Still, it sucks.

I don't know anything about scripting. Would there be a template that I could modify to run multiple mogrify -crop commands from the same directory of images, outputting to a different -path each time?

My project is at:

http://www.momentaryvitality.ca

Thanks.

Re: Running multiple "mogrify" crop commands in sequence?

Posted: 2012-06-11T20:23:59-07:00
by fmw42
what version of IM are you using and on what platform? bash unix shell scripting can be used to run a sequence of mogrify commands. But I am not sure that is the best way. You may be better off using convert with multiple crops on a single image and creating an output image for each crop. Either way, a bash shell script could do that.

Can you explain a bit better or provide an example image and the crop regions you need for it? Do the crop regions stay the same over time for that image sequence?

Re: Running multiple "mogrify" crop commands in sequence?

Posted: 2012-06-11T20:55:41-07:00
by featheredtar
I'm using 6.7.7 x64 on Windows 7, although I might use these new techniques to batch images on a local university's supercomputer which runs on Linux, so that I can use my home computer only for video editing. Windows is the most important for now though.

Here is a (shrunk) initial image of a ~1500 image sequence, with its original height being 28,000 px.

https://dl.dropbox.com/u/11229786/60-Sc ... 1-0095.jpg

My crop parameters for the shrunk image, with the intent of extracting the big yellow flower would be:

mogrify -crop 420x400+365+25 -path 'path' *.jpg

Yes, either of those ways sound good, although I wouldn't know how to tell the convert program to perform the 15 crops on each image in a directory, short of specifying each image.

Re: Running multiple "mogrify" crop commands in sequence?

Posted: 2012-06-11T21:10:54-07:00
by fmw42
So are you only cropping one flower from the image sequence or do you crop each flower in the photo?

mogrify seems like a reasonable way to crop one flower from every image in this sequence. Then do another mogrify for each other flower. That is assuming that the crop parameters never change for each flower.

It is easy to do this in bash unix, but you are on windows. So that is out of my league as I do only bash unix scripting. But I would imagine that you can put a sequence of mogrify commands in a single bat file (one per line) on windows and then just have it go through each mogrify one at a time. That would be how I would do it in a bash shell script.

For windows scripting of IM commands and windows syntax differences, see
http://www.imagemagick.org/Usage/windows/

By the way you could install cygwin on your windows system and then IM for cygwin and then run unix shell scripts.

Re: Running multiple "mogrify" crop commands in sequence?

Posted: 2012-06-11T21:45:01-07:00
by featheredtar
I crop each flower from the sequence to make individual image sequences. Right, the parameters for each flower never change.

I didn't know making a simple .bat script was so easy. That's fantastic. What I did as a test was:

G:
cd test
mogrify -path F:\test1 -crop 1200x1850+5328+1166 *.jpg
cd \test2
mogrify -path F:\test2 -crop 2200x850+5328+1166 *.jpg

Would I be doing bash unix scripting if I were to do it in Linux? And three more questions. Firstly, how do you make the resulting files take a name followed by a numbering sequence that has a consistent number of digits? Secondly, is there a way for the resulting files to retain the original timestamp of the original raw scan files? Thirdly, does ImageMagick have a way to do lossless JPEG cropping?

Thanks so much!

Re: Running multiple "mogrify" crop commands in sequence?

Posted: 2012-06-12T10:38:16-07:00
by fmw42
Would I be doing bash unix scripting if I were to do it in Linux? And three more questions. Firstly, how do you make the resulting files take a name followed by a numbering sequence that has a consistent number of digits? Secondly, is there a way for the resulting files to retain the original timestamp of the original raw scan files? Thirdly, does ImageMagick have a way to do lossless JPEG cropping?
1) bash unix scripts are very similar. I presume you have images in test1 and test and want to save them to test1a and test2a


#!/bin/bash
cd test1
mogrify -path pathto/test1a -crop 1200x1850+5328+1166 *.jpg
cd \test2
mogrify -path pathto/test2a -crop 2200x850+5328+1166 *.jpg


2) In mogrify, I believe that the resulting names are always the same as the input. So you need to rename your input images to put some number on them.

However, with convert, you can do

convert image1 image2 image3 ... imageN result_%02d.jpg

where the %02d will append 01, 02, 03 ... 10 ...99
and %03d will be 001, 002 ...099


Try this for a test:
convert rose: /pathto/test1/rose1.jpg
convert rose: /pathto/test1/rose2.jpg
convert rose: /pathto/test1/rose2.jpg

cd test1
convert *.jpg -crop 20x20+20+20 +repage /pathto/test2/rose_%02d.jpg

Change the path descriptions for windows as appropriate. I presume windows also supports %02d construct, but don't really know. If it does, then you may need to use %%02d to escape the % as %%.

The main issue here is that mogrify will process each image one at a time, whereas, convert will try to load all the images at once. Thus you may run out of memory or need to configure IM to for large memory requirements. see http://www.imagemagick.org/Usage/files/#massive. Alternately, will convert, you could write a script that loops over each image in your directory and does the crop on it.

3) I am not sure about keeping the timestamp. Are you talking about an EXIF timestamp or a filesystem timestamp?

4) JPG does not support lossless writing of files. The best you can do is set -quality 100, but it is still a little lossy. For lossless jpg, you would need to use JP2000 via the Jasper delegate library. That is you need to install the Jasper delegate library and then recompile IM. But I don't really know how to do that in Windows, though I have installed it for my Mac system. Alternately, output to png or tiff or some other non-compressed format.

Re: Running multiple "mogrify" crop commands in sequence?

Posted: 2012-06-13T17:16:16-07:00
by featheredtar
I'm on my Mac at the moment, so I can't try that right now.

Ah yes, each sequence from which I extract a flower can be around 100GB. So if a "convert *.jpg" would load all of those images, I guess it wouldn't work.

Either I guess. I ask because XNConvert has the option of retaining the filesystem or EXIF timestamp. Ok. I scan them at 90% quality, and convert them to 94% quality, so I'm assuming there's not too much quality loss with a lossy crop. I wish I could do PNG or TIFF, but I don't have the money to by that much storage yet unfortunately. Saving in lossless would also make editing a big pain. Dealing with 10,000x10,000 TIFF time lapse sequences would be horrible! I guess I could work with proxy media though.

Speaking of compiling, I've been wondering how to do this with libjpeg-turbo

viewtopic.php?f=1&t=21218