Newbie Batch Processing Question

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
OldBoy
Posts: 8
Joined: 2011-01-11T14:13:36-07:00
Authentication code: 8675308
Location: Oxfordshire, UK

Newbie Batch Processing Question

Post by OldBoy »

Hi there

My question has 2 parts - it should be simple to answer to anyone with simple batch-processing experience. I'm using latest version for Windows, and despite my efforts, research and testing, I am failing miserably, now with a crappy headache ...

Firstly, I want to apply Fred's Quadrant Crop function(http://www.fmwconcepts.com/imagemagick/ ... .php#crop2) to an entire directory of numbered images (filename####.png). The parts need to be 320 by 200 (so as to be processed in an old obscure retro imaging app which doesn't support files smaller or larger).

What piece of code / correct syntax / scripting do I need for this?

Secondly, after all the images are processed, I will need to put them back together. There is a solution here: http://studio.imagemagick.org/discourse ... t=0#p51611

Again, what would be the correct piece of code / syntax / scripting do I need to make this happen? Also, the files should all be in the same directory.

Putting a .bat file is obvious, but the code inside is where I am failing, or the command structure. Hopefully there should be a way of processing every image at a time and outputting the parts with appropriate suffixes.

Many thanks for your time if you have read this or can help in anyway.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Newbie Batch Processing Question

Post by fmw42 »

You will need to write a script to traverse your directory. IM has mogrify, but it works on one directory of images and makes one output for every input.

see Anthony's examples pages for the use of crop at http://www.imagemagick.org/Usage/crop/#crop

For windows difference in IM and batch scripting help, see http://www.imagemagick.org/Usage/windows/
Last edited by fmw42 on 2011-01-11T14:55:44-07:00, edited 1 time in total.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Newbie Batch Processing Question

Post by Bonzo »

I am not sure you can run Freds code on Windows without installing Cgwin ( I tried it and hated it ) or something similar.

You should be able to write something using Imagemagick and batch files; I have some batch file examples here but not exactly what you want: http://www.rubblewebs.co.uk/imagemagick/batch.php but it may give you some ideas.
Last edited by Bonzo on 2011-01-11T14:53:03-07:00, edited 1 time in total.
OldBoy
Posts: 8
Joined: 2011-01-11T14:13:36-07:00
Authentication code: 8675308
Location: Oxfordshire, UK

Re: Newbie Batch Processing Question

Post by OldBoy »

fmw42 wrote:You will need to write a script to traverse your directory. IM has mogrify, but it works on one directory of images and makes one output for every input.

For windows difference in IM and batch scripting help, see http://www.imagemagick.org/Usage/windows/
Thanks for the quick reply :)

I have looked at the windows usage page (and others), yet I keep getting errors (cannot find file) or nothing at all. Trying to use some kind of variables are not working. Admittedly, I'm very much a newbie at Image Magick. I don't know where to restart ... :(
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Newbie Batch Processing Question

Post by fmw42 »

Bonzo,

the code he is referring to is not a script but a single command line from the tidbits page at http://www.fmwconcepts.com/imagemagick/ ... .php#crop2

So it is just a matter of windows batch scripting to traverse his directories and applying the command with appropriate output names.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Newbie Batch Processing Question

Post by fmw42 »

I have looked at the windows usage page (and others), yet I keep getting errors (cannot find file) or nothing at all. Trying to use some kind of variables are not working. Admittedly, I'm very much a newbie at Image Magick. I don't know where to restart ...
Perhaps you should post your batch file code and other windows users can critique it.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Newbie Batch Processing Question

Post by Bonzo »

Sorry Fred I didn't check the link :?
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Newbie Batch Processing Question

Post by Bonzo »

The code below works for the first step BUT you can end up with some odd size images if the original size is not equaly divisable by the crop size.

Code: Select all

::Turn of displaying the code on the screen
@echo off

:: Read all the jpg images from the directory and crop them - different save directory can be specified.
:: In my case the bat file was in the user directory and the images to crop were in a folder on the desktop. I also saved to the same directory.
:: Command run = C:\Users\Anthony\crop.bat C:\Users\Anthony\Desktop\crop_test C:\Users\Anthony\Desktop\crop_test
for %%f in (%1\*.jpg) do ( convert "%%f" -crop 128x128 +repage "%2\mandril_128x128_%d.jpg" )
OldBoy
Posts: 8
Joined: 2011-01-11T14:13:36-07:00
Authentication code: 8675308
Location: Oxfordshire, UK

Re: Newbie Batch Processing Question

Post by OldBoy »

Thanks Bonzo

I tried something very similar to that (without the brackets). Unfortunately, by putting the code in and changing the parameters of what you entered, absolutely nothing happens :(

I guess I'm being really thick or biting off more than I can chew at this stage. It could be an issue with the png format, or maybe Im not declaring some variables (the only time I have done this is with the PASCAL language 16 years ago - in comparison, MSDOS seems far more confusing!)
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Newbie Batch Processing Question

Post by anthony »

NOTE that was not a quadrant crop. It was a tile crop with large tiles.

For a quadrant crop around a specific point see...
http://www.imagemagick.org/Usage/crop/#crop_quad

For the best near-equal division of an image into 4 (2x2) tiles you can use the special '@' flag to request the crop by number of divisions.
EG: -crop 2x2@ +repage

See Cropping into roughly Equally Sized Divisions
http://www.imagemagick.org/Usage/crop/#crop_equal
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
OldBoy
Posts: 8
Joined: 2011-01-11T14:13:36-07:00
Authentication code: 8675308
Location: Oxfordshire, UK

Re: Newbie Batch Processing Question

Post by OldBoy »

Thanks Anthony - I actually missed that.

Well, it looks like it isn't a quad cut that I need, just equally sized divisions.

I've tried using a lateral method to help put together a .bat in a linear way. I've used a spreadsheet editor to reproduce the command for each file in the folder:

Code: Select all

convert example0000.png -crop 320x200 +repage +adjoin example0000_%d.png
convert example0001.png -crop 320x200 +repage +adjoin example0001_%d.png
etc....
In this folder there are 3500+ images that I would like to split. They are all 640 by 480, so based on the size I need to reduce them to, there should be 6 parts to every image.

When I ran the .bat, something finally happened, but didn't work as expected. Instead of splitting the image into 6 parts, there was only one output for each image (in the same folder) named example####_d.png (# representing the number, and NO numerical extension for each new file were the %d is, ), and I am presuming they were the remainder of each process, not parts.

Any ideas?

EDIT: adding _%02d.png at the end of the output filename produced nothing at all :(
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Newbie Batch Processing Question

Post by Bonzo »

Why not start with something simple; 1 or 2 files not the whole 3500.

You have +adjoin in the command - this is used to join up images.

I do not know if putting the output in " " has an effect but if you do not you may need to escape the % with another % so it becomes example0001_%%d.png
OldBoy
Posts: 8
Joined: 2011-01-11T14:13:36-07:00
Authentication code: 8675308
Location: Oxfordshire, UK

Re: Newbie Batch Processing Question

Post by OldBoy »

Hi Bonzo

I have tried your suggestion of getting one to work. Still the same unwanted output.

I don't know why the adjoin command is there, but I am using this example: http://www.imagemagick.org/Usage/crop/#crop_tile
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Newbie Batch Processing Question

Post by Bonzo »

I have done a couple of test and to crop the image keeping the original image name but adding a number to it this works. It works for me both with jpg and png images - note with this code you could convert the output images from png to jpg if you wanted.
Example input file name example.png
Output filename in the format example_0.png

Code: Select all

for %%f in (%1\*.png) do ( convert "%%f" -crop 128x128 +repage "%2\%%~nf_%%d.png" )
Again I ran it using C:\Users\Anthony\crop.bat C:\Users\Anthony\Desktop\crop_test C:\Users\Anthony\Desktop\crop_test

Notice I have escaped the % in the file names - it is a while since I tried using batch files and I can not remember 100% what things do :(

Supposedly putting " " around input and output filename paths allows you to use spaces in the names but that never seemed to work for me so all my paths etc. do not have spaces.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Newbie Batch Processing Question

Post by fmw42 »

Some of the tile cropping is relatively new. What version of IM are your using?
Post Reply