Multi crop and multi save in Windows batch

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?".
Post Reply
saucedog
Posts: 8
Joined: 2014-08-27T05:48:59-07:00
Authentication code: 6789

Multi crop and multi save in Windows batch

Post by saucedog »

I have a 1280x1024 screenshot and I want to take uniform 62x138 sized crops of the image in all different places (already have the coordinates), and then save those crops away as smaller individual images for manipulation later.
I've used batch files to join many smaller images into huge ones before. But I'm trying to do the exact opposite and neatly deconstruct a large image. I already have every command and crop and chop in the full sequence written out -- and it works correctly when running the commands one at a time in command prompt. But I don't know enough about the syntax to get it to work in batch file, apparently. I'm getting errors of "geometry does not contain image" exclusively. An example of lines from the bat:

Code: Select all

convert e:\01.png -crop 62x138+283+141 e:\cropa5.png 
convert e:\01.png -crop 72x138+0+281 e:\cropb.png 
convert e:\01.png -crop 62x138+78+281 e:\cropb2.png 
convert e:\01.png -crop 62x138+146+281 e:\cropb3.png 
I've tried mogrify instead of convert, tried using carets at the end of lines, ( ) on every line, " " for the paths and all kinds of other ignorant but maybe-it-will-work-this-time things. Just errors (and some incorrect cropping -- like 4 of the 50 commands work and they mostly just destroy the image).
Can anyone help me sort this out?
Also, I've never used powershell or cygwin in any great amount (that's probably obvious) but I'm open to doing that if I can't do this with a batch file.
Thanks.

edit version 6.7.0-3
Last edited by saucedog on 2014-08-27T06:24:25-07:00, edited 1 time in total.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Multi crop and multi save in Windows batch

Post by snibgo »

Those four commands will be fine in a .BAT batch file, provided e:\01.png exists and is large enough. "geometry does not contain image" would mean your coordinates were outside the input image.
snibgo's IM pages: im.snibgo.com
saucedog
Posts: 8
Joined: 2014-08-27T05:48:59-07:00
Authentication code: 6789

Re: Multi crop and multi save in Windows batch

Post by saucedog »

Thanks for the quick response. That's my confusion as well. They all actually *do* work just fine when I run each line manually, in succession, in CMD. But when I run the batch, most of them error out. Is there a lock time on the parent image file that I need to wait for in between convert commands in the batch? Thx again.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Multi crop and multi save in Windows batch

Post by snibgo »

Your version of IM is ancient. I suggest you upgrade.

As the input is only read, not written, locking shouldn't be an issue. What version of Windows are you on? You could try a "timeout /T 2" command between each convert.

What does "identify -verbose e:\01.png" say?
snibgo's IM pages: im.snibgo.com
saucedog
Posts: 8
Joined: 2014-08-27T05:48:59-07:00
Authentication code: 6789

Re: Multi crop and multi save in Windows batch

Post by saucedog »

identify dumps a whole bunch of info. I'll update my version. Don't know if it matters, but this is on a VM, Win7 x86, and the E drive is a shared drive and is ultimately a network location. My work day just started so I have to black out for 9 hours. I'll try to update this later. Thanks for the advice, snibgo.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Multi crop and multi save in Windows batch

Post by snibgo »

I know nothing about VM and network drives, but doing 50 of these, re-reading the input file 50 times over a network, will give performance problems and might cause locking problems. I would make a local copy, and do subsequent work on that. For example:

Code: Select all

convert e:\01.png local01.miff
convert local01.miff -crop 62x138+283+141 e:\cropa5.png
convert local01.miff -crop 72x138+0+281 e:\cropb.png
convert local01.miff -crop 62x138+78+281 e:\cropb2.png
convert local01.miff -crop 62x138+146+281 e:\cropb3.png
del local01.miff
(.miff is IM's own format, and faster to read than .png.)

Does the second crop have a typo? Should "72" be "62"?

A number of things in "identify verbose" might show a problem. You can paste the output here. Or upload the input somewhere like dropbox.com and paste the URL here.
snibgo's IM pages: im.snibgo.com
saucedog
Posts: 8
Joined: 2014-08-27T05:48:59-07:00
Authentication code: 6789

Re: Multi crop and multi save in Windows batch

Post by saucedog »

It's not a typo, unfortunately. I don't know why that value needed to be different than the others, but the x-value of 72 on that command and the x-value of 62 on the previous command create a crop the exact same size. I just accepted that it's goofy and moved on.

So it's working *better* when using local resources and pares down the image correctly. But it's still not cropping and saving the smaller pieces of the image.

Here are 4 files:

1. The batch file with local resources and output.
https://www.dropbox.com/s/0k2ql2yxxdsoo ... e.bat?dl=0
2. Identify results
https://www.dropbox.com/s/y9fwtcqx5dc6n ... y.txt?dl=0
3. Original Image
Image
4. Current output
Image


All told, it should retain the cropped output image as 01.png and then crop it 20 times to distribute it as 20 new, smaller images... uniform rectangles with unique names.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Multi crop and multi save in Windows batch

Post by snibgo »

"identify" says e:\01.png is size 74x88 pixels. That's why most of your crops fail.

I'm not familiar with mogrify, and I don't know why you are reading and writing the same file so many times. Perhaps this single command does you want for the initial chopping and cropping:

Code: Select all

convert c:\test\01.png -chop 0x97 ^
-crop 668x745+0 ^
-chop 79x0+520+0 ^
-chop 79x0+372+0 ^
-chop 79x0+224+0 ^
-chop 79x0+76+0 ^
-crop 344x745+10+0 ^
-chop 0x61+0+139 ^
-chop 0x61+0+278 ^
-chop 0x61+0+417 ^
t.png
Then you can chop the result into 20 pieces:

Code: Select all

convert t.png -crop 5x4@ t_%%02d.png
Or do it all in a single command:

Code: Select all

convert c:\test\01.png -chop 0x97 ^
-crop 668x745+0 ^
-chop 79x0+520+0 ^
-chop 79x0+372+0 ^
-chop 79x0+224+0 ^
-chop 79x0+76+0 ^
-crop 344x745+10+0 ^
-chop 0x61+0+139 ^
-chop 0x61+0+278 ^
-chop 0x61+0+417 ^
-crop 5x4@ t_%%02d.png
snibgo's IM pages: im.snibgo.com
saucedog
Posts: 8
Joined: 2014-08-27T05:48:59-07:00
Authentication code: 6789

Re: Multi crop and multi save in Windows batch

Post by saucedog »

Works perfectly, man. Thanks for your help. I should apologize for being programmatically deficient. Take care and thanks again.
Post Reply