Rotating, merging and cropping batch of images

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
Koozer
Posts: 4
Joined: 2017-03-16T07:05:36-07:00
Authentication code: 1151

Rotating, merging and cropping batch of images

Post by Koozer »

Hi there,

I have the need to rotate pairs of images, stitch them horizontally and then crop to a certain size. Currently I'm doing this one step at a time and producing lots of excess images as my attempts to pipe outputs is failing me. I'm trying to do something like the following in Powershell:

magick (convert "d:\a folder with spaces\01022017083759_01.jpg" -rotate 270 -quality 100 "d:\a folder with spaces\rotated\01022017083759_01_rot270.jpg") (convert "d:\a folder with spaces\01022017083759_02.jpg" -rotate 90 -quality 100 "d:\a folder with spaces\rotated\01022017083759_02_rot90.jpg") "d:\a folder with spaces\rotated\01022017083759_02_rot90.jpg" "d:\a folder with spaces\rotated\01022017083759_01_rot270.jpg" +append miff:- | magick - -crop 7633x5767+1975.5+1460.5 +repage "d:\a folder with spaces\cropped\01022017083759_cropped.jpg"

This command runs, but seems to just go forever, hogging CPU resources. Each individual command works fine on its own, outputting to jpegs. What am I doing wrong?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Rotating, merging and cropping batch of images

Post by snibgo »

Do you want to keep the intermediate files? Why are they JPG format? I don't see why you are piping at all. There is only one image, so that can be cropped in the same command as the append.

If you don't need the intermediates, then this does the job. For clarity, I simplify the filenames.

Code: Select all

magick ( 9_01.jpg -rotate 270 ) ( 9_02.jpg -rotate 90 ) +append -crop 7633x5767+1975.5+1460.5 +repage out.tiff
If you do want to keep the intermediates, insert "+write temp_rotX.jpg" at any places you want within that command.
snibgo's IM pages: im.snibgo.com
Koozer
Posts: 4
Joined: 2017-03-16T07:05:36-07:00
Authentication code: 1151

Re: Rotating, merging and cropping batch of images

Post by Koozer »

Thanks for the suggestion, but I get an error on the rotation when I try that:

Code: Select all

You must provide a value expression on the right-hand side of the '-' operator.
At line:1 char:
+ magick ( "01022017083759_01.jpg" - <<<< rotate 270 ) ( "01022017083759_02.jpg" -rotate 90 ) +append
 -crop 7633x5767+1975.5+1460.5 +repage "cropped\01022017083759_cropped.jpg"
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : ExpectedValueExpression
I am not interested in the intermediary images, I only create them at the moment as it's the only way I can get the process to work!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Rotating, merging and cropping batch of images

Post by snibgo »

The version I showed is for the Windows command line. Sorry, I don't use Powershell, and don't know how to write it in a way that Powershell understands, or what escapes Powershell might need for the parentheses etc.
snibgo's IM pages: im.snibgo.com
Koozer
Posts: 4
Joined: 2017-03-16T07:05:36-07:00
Authentication code: 1151

Re: Rotating, merging and cropping batch of images

Post by Koozer »

Ah so it's a Powershell issue not Imagemagick? That narrows it down at least, thanks!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Rotating, merging and cropping batch of images

Post by snibgo »

Yes, the error messages are coming from Powershell. Sorry, I can't help with that. When you get it working, I encourage you to post it here, to help other people who use IM in Powershell.
snibgo's IM pages: im.snibgo.com
Koozer
Posts: 4
Joined: 2017-03-16T07:05:36-07:00
Authentication code: 1151

Re: Rotating, merging and cropping batch of images

Post by Koozer »

So Powershell was reading the brackets as special characters rather than literals, which was easy enough to solve by escaping with `. The bit that stumped me was that it still didn't work for some mysterious reason. Turns out Imagemagick doesn't like it if your brackets don't have spaces around them...

The following ridiculously simple line now works perfectly in Powershell:

Code: Select all

magick 'img1.png' -rotate 90 `( 'img2.png' -rotate 270 `) +append -crop 124x62+62+31 +repage 'out.png'
Post Reply