Cut+paste from two regions of one image..

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
that99thguy
Posts: 2
Joined: 2014-07-02T08:39:41-07:00
Authentication code: 6789

Cut+paste from two regions of one image..

Post by that99thguy »

Hi everybody --

So I have a time-pressed project that's giving me huge headaches.. I've searched the forums and have tried so many different ways to accomplish this, but I'm just stuck. I'm trying to go thru all pdfs in a folder, convert to jpg and rotate 90 degrees., then I need to cut and paste two small regions of that jpg onto the same image, and finally crop and resize it. I have the following batch file (latest attempt):

Code: Select all

@ECHO OFF
FOR %%f IN ("C:\EKG_Images\*.pdf") DO (
	ECHO.Converting %%f...
	.\im\convert -density 400 -rotate 90 "%%f" "C:\EKG_Images\%%~nf.jpg"

	IF EXIST "C:\EKG_Images\%%~nf.jpg" (
		ECHO.Relocating sidebars...
		.\im\convert "C:\EKG_Images\%%~nf.jpg" ( +clone -crop 60x3800+40+75 ) -geometry +170+90 ( +clone -crop 60x620+3340+3700 ) -geometry +170+3600 -composite "C:\EKG_Images\%%~nf.jpg"

  		ECHO.Cropping and resizing JPG...
	    	.\convert "C:\EKG_Images\%%~nf.jpg" -crop 3190x4200+140+60 -density 400 -resize 2700 "C:\EKG_Images\%%~nf.jpg"
	) ELSE (
		ECHO.*** ERROR *** Could not convert pdf to jpg.
		GOTO done
	)
)
ECHO.
:done
ECHO.Done.
But oddly, the echo "Converting" doesn't even show; it immediately displays the error "-geometry was unexpected at this time." I'm really at a wall trying to figure out how to make this happen, and my boss is getting impatient. Can anyone lend a hand on this? Thanks..
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Cut+paste from two regions of one image..

Post by snibgo »

The error message is from Windows the interpretor, which can't make sense of your command. The problem is almost certainly the parentheses within the convert command, which is nested in a "IF ( ...)". The close-parenthesis in the convert closes the IF, so Windows thinks "-geometry" must start a new command.

I cure this by escaping each open and close parens with caret ^.

Thus:

Code: Select all

.\im\convert "C:\EKG_Images\%%~nf.jpg" ^( +clone -crop 60x3800+40+75 ^) -geometry +170+90 ^( +clone -crop 60x620+3340+3700 ^) -geometry +170+3600 -composite "C:\EKG_Images\%%~nf.jpg"
An alternative is to put each one in double-quotes.
snibgo's IM pages: im.snibgo.com
that99thguy
Posts: 2
Joined: 2014-07-02T08:39:41-07:00
Authentication code: 6789

Re: Cut+paste from two regions of one image..

Post by that99thguy »

Dahhhhhhh!!!! Thank you, snibgo!!! Apologies to the forum that this wasn't an Imagemagick problem, per se.

For anyone curious tho, I also needed to add "-composite" after each "-geometry ..." and that did it!

Thanks again for the quick response! :D
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Cut+paste from two regions of one image..

Post by snibgo »

Another couple of tips:

1. Don't use "echo off" until you've debugged the command file.

2. IM lossy-compresses each JPG write. You write a JPG, read it, write it, read it and finally write it again. Each write will mangle the pixels. At the very least, I suggest you use a non-lossy format (eg TIFF or PNG) for the two intermediates.

3. See my sig line for loads of examples. Not necessarily good ones, but may give you some tips for IM under Windows.
snibgo's IM pages: im.snibgo.com
Post Reply