Resizing and cropping one image multiple times in one comman

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
blindcoder
Posts: 8
Joined: 2013-06-14T00:10:37-07:00
Authentication code: 6789

Resizing and cropping one image multiple times in one comman

Post by blindcoder »

Hello.

I have another question about using a single command for multiple operations:
I want to crop an image into tiles, then resize that image, then crop the result again. Right now I have this code:

Code: Select all

convert -monitor ../output.jpg \
-write mpr:output \
\( mpr:output -crop 256x256 -set filename:tile "%[fx:page.x/256]_%[fx:page.y/256]" output_files/14/%[filename:tile].jpg \) \
\( mpr:output -resize 50% -write mpr:output \) \
\( mpr:output -crop 256x256 -set filename:tile "%[fx:page.x/256]_%[fx:page.y/256]" output_files/13/%[filename:tile].jpg \)
At the end of the command I get the following error message:

Code: Select all

convert: unable to open image `output_files/14/%[filename:tile].jpg':  @ error/blob.c/OpenBlob/2587.
convert: unable to open image `output_files/13/%[filename:tile].jpg':  @ error/blob.c/OpenBlob/2587.
convert: unbalanced parenthesis `)' @ error/convert.c/ConvertImageCommand/3007.
I don't understand either of those error messages. Shouldn't %[filename:tile] be replaced by "0_0" "0_1" etc and the result of the crop be saved to disk? No output files are generated at all.
Also, there are three opening and three closing brackets, so how can they be unbalanced?

Kind regards,
Benjamin
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Resizing and cropping one image multiple times in one co

Post by Bonzo »

What platform are you running this on?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Resizing and cropping one image multiple times in one co

Post by snibgo »

Does output_files/14/%[filename:tile].jpg exist before you run the command? If it doesn't, the command will fail, because you have asked to read it. Perhaps you wanted to write it; use "-write".

A convert command should never end in a close bracket. It should end with an output filename. If you want to discard the result, use "NULL:".
snibgo's IM pages: im.snibgo.com
blindcoder
Posts: 8
Joined: 2013-06-14T00:10:37-07:00
Authentication code: 6789

Re: Resizing and cropping one image multiple times in one co

Post by blindcoder »

I'm running this on Ubuntu 12.04 with IM 6.6.9-7 2013-06-09 Q8

Maybe I asked it to read it, but I did not mean to do that. I meant for convert to write cropped tiles of 256x256 px to 0_0.jpg 0_1.jpg etc. Like it would with:

Code: Select all

convert -monitor ../output.jpg -crop 256x256 -set filename:tile "%[fx:page.x/256]_%[fx:page.y/256]" output_files/14/%[filename:tile].jpg
Adding NULL: to the end gets rid of the "unbalanced parenthesis" error, but still does not output any files in output_files/{13,14}.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Resizing and cropping one image multiple times in one co

Post by snibgo »

The basic syntax of the convert command is:

Code: Select all

convert {input_file} {processing} {output_file}
If you want to write in other places, you need the "-write" command. As you did for the mpr outputs.
snibgo's IM pages: im.snibgo.com
blindcoder
Posts: 8
Joined: 2013-06-14T00:10:37-07:00
Authentication code: 6789

SOLVED: Resizing and cropping one image multiple times in on

Post by blindcoder »

snibgo wrote:The basic syntax of the convert command is:

Code: Select all

convert {input_file} {processing} {output_file}
If you want to write in other places, you need the "-write" command. As you did for the mpr outputs.
Not sure if you meant to say this, but with this hint I managed to get it working like this:

Code: Select all

convert -monitor ../output.jpg \
-write mpr:output \
\( mpr:output -crop 256x256 -set filename:tile "%[fx:page.x/256]_%[fx:page.y/256]" -write output_files/14/%[filename:tile].jpg \) \
\( mpr:output -resize 50% -write mpr:output \) \
\( mpr:output -crop 256x256 -set filename:tile "%[fx:page.x/256]_%[fx:page.y/256]" -write output_files/13/%[filename:tile].jpg \)
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Resizing and cropping one image multiple times in one co

Post by snibgo »

Yes, that's it. However, you don't have a final output file, so you should add "NULL:" to the end. Alternatively, change your last line to remove the parentheses and "-write".

After your resize, you are writing the mpr then immediately reading it again. This is superfluous.
snibgo's IM pages: im.snibgo.com
blindcoder
Posts: 8
Joined: 2013-06-14T00:10:37-07:00
Authentication code: 6789

Re: Resizing and cropping one image multiple times in one co

Post by blindcoder »

snibgo wrote:Yes, that's it. However, you don't have a final output file, so you should add "NULL:" to the end. Alternatively, change your last line to remove the parentheses and "-write".

After your resize, you are writing the mpr then immediately reading it again. This is superfluous.
Ah, yes, I forgot to post the NULL:, the laptop I'm running convert on is not the same as the one I'm posting from.
I don't understand your second sentence, though. I thought I need to store the resized image somewhere?
I'm repeating this for /12, /11, /10, ..., /0 so I'm scaling the image down 14 times and each time I -crop the resulting, resized image.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Resizing and cropping one image multiple times in one co

Post by snibgo »

The sequence ...

Code: Select all

-resize 50% -write mpr:output \) \
\( mpr:output -crop 256x256
... can be replaced with ...

Code: Select all

-resize 50% -crop 256x256
snibgo's IM pages: im.snibgo.com
blindcoder
Posts: 8
Joined: 2013-06-14T00:10:37-07:00
Authentication code: 6789

Re: Resizing and cropping one image multiple times in one co

Post by blindcoder »

snibgo wrote:The sequence ...

Code: Select all

-resize 50% -write mpr:output \) \
\( mpr:output -crop 256x256
... can be replaced with ...

Code: Select all

-resize 50% -crop 256x256
But only for the last one, right? Or is it automatically written back to mpr:output?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Resizing and cropping one image multiple times in one co

Post by snibgo »

The replacement code I showed will work for the command you posted. Nothing is automatically written to mpr:output.
snibgo's IM pages: im.snibgo.com
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Resizing and cropping one image multiple times in one co

Post by snibgo »

And you start off by writing to mpr:output then reading it. I can see you are reading that same image again before the resize, but then overwrite it before reading it again.

I suspect "clone" would be useful here, to simplify the command, use less memory and perhaps speed it up. If you post the entire command, perhaps I can advise further.
snibgo's IM pages: im.snibgo.com
Post Reply