How to crop margin in imagemagick?

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?".
VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

Re: How to crop margin in imagemagick?

Post by VanGog »

The first command from you still does not work. I gives still the same errors that "No such file or directory"

Code: Select all

If you're gonna use this code in batch file, you must use double % instead sinle
  which is used in command line CMD!

D:\Europe>for /R %F in (*.pdf) do "c:\program files\ImageMagick-
6.7.5-Q8\convert.exe" -density 400 -colorspace RGB "%F" -background white -alpha
 remove -shave 30%x30% -quality 34% "%~dF%~pF%~nF.jpg"

D:\Europe>"c:\program files\ImageMagick-6.7.5-Q8\convert.exe" -d
ensity 400 -colorspace RGB "D:\Europe\Austria\lower.pdf" -background white
-alpha remove -shave 30%x30% -quality 34% "D:\Europe\Austria\lower.jpg"

D:\Europe>"c:\program files\ImageMagick-6.7.5-Q8\convert.exe" -d
ensity 400 -colorspace RGB "D:\Europe\Austria\upper.pdf" -background white
-alpha remove -shave 30%x30% -quality 34% "D:\Europe\Austria\upper.jpg"

D:\Europe>"c:\program files\ImageMagick-6.7.5-Q8\convert.exe" -d
ensity 400 -colorspace RGB "D:\Europe\Belgium\lower.pdf" -background white
-alpha remove -shave 30%x30% -quality 34% "D:\Europe\Belgium\lower.jpg" -de
nsity 400 -colorspace RGB "D:\Europe\Austria\upper.pdf" -background white -
alpha remove -shave 30%x30% -quality 34% "D:\Europe\Austria\upper.jpg"
convert.exe: unable to open image `D:\Europe\Belgium\lower.jpg': No such fi
le or directory @ error/blob.c/OpenBlob/2614.

D:\Europe>"c:\program files\ImageMagick-6.7.5-Q8\convert.exe" -d
ensity 400 -colorspace RGB "D:\Europe\Belgium\upper.pdf" -background white
-alpha remove -shave 30%x30% -quality 34% "D:\Europe\Belgium\upper.jpg" -de
nsity 400 -colorspace RGB "D:\Europe\Austria\upper.pdf" -background white -
alpha remove -shave 30%x30% -quality 34% "D:\Europe\Austria\upper.jpg"
convert.exe: unable to open image `D:\Europe\Belgium\upper.jpg': No such fi
le or directory @ error/blob.c/OpenBlob/2614.

D:\Europe>"c:\program files\ImageMagick-6.7.5-Q8\convert.exe" -d
ensity 400 -colorspace RGB "D:\Europe\Belgium\upper_enr3.pdf" -background w
hite -alpha remove -shave 30%x30% -quality 34% "D:\Europe\Belgium\upper_enr
3.jpg" -density 400 -colorspace RGB "D:\Europe\Austria\upper.pdf" -backgrou
nd white -alpha remove -shave 30%x30% -quality 34% "D:\Europe\Austria\upper
.jpg"
convert.exe: unable to open image `D:\Europe\Belgium\upper_enr3.jpg': No su
ch file or directory @ error/blob.c/OpenBlob/2614.

D:\Europe>"c:\program files\ImageMagick-6.7.5-Q8\convert.exe" -d
ensity 400 -colorspace RGB "D:\Europe\Croatia_(Chorvatsko)\lower.pdf" -back
ground white -alpha remove -shave 30%x30% -quality 34% "D:\Europe\Croatia_(
Chorvatsko)\lower.jpg" -density 400 -colorspace RGB "D:\Europe\Austria\uppe
r.pdf" -background white -alpha remove -shave 30%x30% -quality 34% "D:\MAPY\Euro
pe\Austria\upper.jpg"
convert.exe: unable to open image `D:\Europe\Croatia_(Chorvatsko)\lower.jpg
': No such file or directory @ error/blob.c/OpenBlob/2614.

D:\Europe>"c:\program files\ImageMagick-6.7.5-Q8\convert.exe" -d
ensity 400 -colorspace RGB "D:\Europe\Croatia_(Chorvatsko)\upper.pdf" -back
ground white -alpha remove -shave 30%x30% -quality 34% "D:\Europe\Croatia_(
Chorvatsko)\upper.jpg" -density 400 -colorspace RGB "D:\Europe\Austria\uppe
r.pdf" -background white -alpha remove -shave 30%x30% -quality 34% "D:\MAPY\Euro
pe\Austria\upper.jpg"
convert.exe: unable to open image `D:\Europe\Croatia_(Chorvatsko)\upper.jpg
': No such file or directory @ error/blob.c/OpenBlob/2614.

D:\Europe>"c:\program files\ImageMagick-6.7.5-Q8\convert.exe" -d
ensity 400 -colorspace RGB "D:\Europe\Cyprus\enroute.pdf" -background white
 -alpha remove -shave 30%x30% -quality 34% "D:\Europe\Cyprus\enroute.jpg" -
density 400 -colorspace RGB "D:\Europe\Austria\upper.pdf" -background white
 -alpha remove -shave 30%x30% -quality 34% "D:\Europe\Austria\upper.jpg"

   **** Warning: File has insufficient data for an image.
libpng error: Write Error
Error: /VMerror in --showpage--
VM status: 3 935695 2253216
Current allocation mode is local
Last OS error: 28
GPL Ghostscript 9.04: Unrecoverable error, exit code 1
convert.exe: `%s': %s "c:/program files/GhostScript9.04/bin/gswin32c.
exe" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dEPS
Crop -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pngalpha" -dTextAlphaBits=4 -dGra
phicsAlphaBits=4 "-r400x400" -g67533x50656  "-sOutputFile=C:/DOCUME~1/ADMINI~1/L
OCALS~1/Temp/magick-_Lzx5dSg-12275968" "-fC:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/mag
ick-oEPfSZJn" "-fC:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/magick-F_EINF75" @ error/pdf
.c/InvokePDFDelegate/199.
convert.exe: Postscript delegate failed `D:\Europe\Cyprus\enroute.pdf': No
such file or directory @ error/pdf.c/ReadPDFImage/667.
convert.exe: unable to open image `D:\Europe\Cyprus\enroute.jpg': No such f
ile or directory @ error/blob.c/OpenBlob/2614.

D:\Europe>"c:\program files\ImageMagick-6.7.5-Q8\convert.exe" -d
ensity 400 -colorspace RGB "D:\Europe\Czech_republic\lower.pdf" -background
 white -alpha remove -shave 30%x30% -quality 34% "D:\Europe\Czech_republic\
lower.jpg" -density 400 -colorspace RGB "D:\Europe\Austria\upper.pdf" -back
ground white -alpha remove -shave 30%x30% -quality 34% "D:\Europe\Austria\u
pper.jpg"
convert.exe: unable to open image `D:\Europe\Czech_republic\lower.jpg': No
such file or directory @ error/blob.c/OpenBlob/2614.

D:\Europe>"c:\program files\ImageMagick-6.7.5-Q8\convert.exe" -d
ensity 400 -colorspace RGB "D:\Europe\Czech_republic\upper.pdf" -background
 white -alpha remove -shave 30%x30% -quality 34% "D:\Europe\Czech_republic\
upper.jpg" -density 400 -colorspace RGB "D:\Europe\Austria\upper.pdf" -back
ground white -alpha remove -shave 30%x30% -quality 34% "D:\Europe\Austria\u
pper.jpg"
   **** Warning: xref subsection header has extra characters.
   **** Warning:  An error occurred while reading an XREF table.
   **** The file has been damaged.  This may have been caused
   **** by a problem while converting or transfering the file.
   **** Ghostscript will attempt to recover the data.
   **** Warning:  There are objects with matching object and generation
   **** numbers.  The accuracy of the resulting image is unknown.

   **** This file had errors that were repaired or ignored.
   **** The file was produced by:
   **** >>>> Acrobat Distiller 6.0 (Windows) <<<<
   **** Please notify the author of the software that produced this
   **** file that it does not conform to Adobe's published PDF
   **** specification.

convert.exe: unable to open image `D:\Europe\Czech_republic\upper.jpg': No
such file or directory @ error/blob.c/OpenBlob/2614.

D:\Europe>"c:\program files\ImageMagick-6.7.5-Q8\convert.exe" -d
ensity 400 -colorspace RGB "D:\Europe\Denmark\lower.pdf" -background white
-alpha remove -shave 30%x30% -quality 34% "D:\Europe\Denmark\lower.jpg" -de
nsity 400 -colorspace RGB "D:\Europe\Austria\upper.pdf" -background white -
alpha remove -shave 30%x30% -quality 34% "D:\Europe\Austria\upper.jpg"
convert.exe: unable to open image `D:\Europe\Denmark\lower.jpg': No such fi
le or directory @ error/blob.c/OpenBlob/2614.

D:\Europe>"c:\program files\ImageMagick-6.7.5-Q8\convert.exe" -d
ensity 400 -colorspace RGB "D:\Europe\Denmark\upper.pdf" -background white
-alpha remove -shave 30%x30% -quality 34% "D:\Europe\Denmark\upper.jpg" -de
nsity 400 -colorspace RGB "D:\Europe\Austria\upper.pdf" -background white -
alpha remove -shave 30%x30% -quality 34% "D:\Europe\Austria\upper.jpg"
convert.exe: unable to open image `D:\Europe\Denmark\upper.jpg': No such fi
le or directory @ error/blob.c/OpenBlob/2614.

D:\Europe>"c:\program files\ImageMagick-6.7.5-Q8\convert.exe" -d
ensity 400 -colorspace RGB "D:\Europe\Estonia\enroute.pdf" -background whit
e -alpha remove -shave 30%x30% -quality 34% "D:\Europe\Estonia\enroute.jpg"
 -density 400 -colorspace RGB "D:\Europe\Austria\upper.pdf" -background whi
te -alpha remove -shave 30%x30% -quality 34% "D:\Europe\Austria\upper.jpg"

   **** Warning: File has unbalanced q/Q operators (too many q's)

   **** This file had errors that were repaired or ignored.
   **** Please notify the author of the software that produced this
   **** file that it does not conform to Adobe's published PDF
   **** specification.
At this point this is worse than the command that I posted here yesterday. My command converted cca 5 files. Your command converted only one file.

If you would like to test the files that I try to convert, they are placed here:
http://www.aircharts.org/enroute/
I try to convert files from Austria, Belgium, Croatia, Cyprus, Czech_republic, Denmark, Estonia... Yet I did not try more files to convert because the Estonia was the last file I tried without success.

Once again the code of mine, that works for at least 5 files:

Code: Select all

for /r %%F in (*.pdf) do ^
"c:\program files\ImageMagick-6.7.5-Q8\convert.exe" -density 400 -shave 30%%x30%% -quality 34%% %%F %%F.jpg
VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

Re: How to crop margin in imagemagick?

Post by VanGog »

Well done. Your last GS command is definitely the best one. It is effective and gives no error. All images are done. You saved me a lot of work. Thanks man.

Except Denmark with is B/W all charts are usable. Just the German chart I will run with greater quality then 34%. There is a lot of very small characters in it.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to crop margin in imagemagick?

Post by fmw42 »

I am not a windows user or bat file programmer. All I can suggest is try your command in command line mode outside the bat file on the image that is causing problems. If it works, then it is in your bat file programming. If it fails, then post a link to your image so others can see if there is something odd about the image and try the commands on it.
Drarakel
Posts: 547
Joined: 2010-04-07T12:36:59-07:00
Authentication code: 8675308

Re: How to crop margin in imagemagick?

Post by Drarakel »

VanGog wrote:The first command from you still does not work.
It didn't work because you changed it - you removed the parentheses. Without them, you get those strange effects.

I have looked at your PDF files. Two files have errors - but Ghostscript can handle them.
The Austria and Croatia files use a smaller CropBox. So, you indeed have to use the cropbox. The "-shave" option wouldn't have worked with most of the files.
The Cyprus PDF is a a special case here, as it's not vector graphics anymore. It's just a 12156x9118 4,67MB JPG file in that PDF. You can read it with ImageMagick/Ghostcript, but better use exactly 72dpi here. Then you sort of get the 'original' JPG.

Normally, this would be a good ImageMagick command now (except for the Cyprus PDF):

Code: Select all

for /r %%F in (*.pdf) do (
 "c:\program files\ImageMagick-6.7.5-Q8\convert.exe" -define pdf:use-cropbox=true -density 400 -colorspace RGB "%%F" -background white -alpha flatten -quality 34 "%%~dF%%~pF%%~nF.jpg"
)
But, as I wrote, ImageMagick sometimes uses wrong (too small) page size values with the cropbox.

So, yeah, stay with the 'Ghostscript loop'.
Drarakel
Posts: 547
Joined: 2010-04-07T12:36:59-07:00
Authentication code: 8675308

Re: How to crop margin in imagemagick?

Post by Drarakel »

VanGog wrote:I don't understand what you mean by that Photoshop has not such low quality. Photoshop saves in 12 levels of quality for JPEG and I can use 4/12 to save in so called "low quality". But those 4/12 quality = 34%.
No. The 4/12 in Photoshop mean ca. 70% for luma and ca. 87% for the two chroma channels - if you want to 'compare' the Photoshop values to the values within ImageMagick. (There is no single 'quality' value anyway. It's just a simple represantion of rather complicated quantization tables.) The lowest setting in Photoshop meant ca. 46% for luma and ca. 81% for chroma - that's the absolute bottom here (with the normal save). Maybe it has changed a bit in the different Photoshop versions - but probably not by much.
VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

Re: How to crop margin in imagemagick?

Post by VanGog »

I will read more about that chanels tomorrow. Now I found I can auto -trim the image because till now I did it by hand. Good idea. But a question here to be added,
if I would use this simplified command for Norway (I apply on *.jpg now in Norway folder):

Code: Select all

for /r %x in (*.jpg) do convert -density 400 -quality 34% -trim "%x" "%x.pg"
It will crop the image's margin from left size and it will end at the point where text is from left side (the text is turned -90°). The text is OK, but it is bad readable, because I would need to have few pixel above the line of text. Just like the height of line would be for example 12px, text is 10px, if you crop it till 10px so it is too much. Any idea how to add e.g. 4px of white margin to the cropped side? Just interest if it is possible.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to crop margin in imagemagick?

Post by fmw42 »

Drarakel
Posts: 547
Joined: 2010-04-07T12:36:59-07:00
Authentication code: 8675308

Re: How to crop margin in imagemagick?

Post by Drarakel »

I guess you just want to readd a border after trimming. That's "-bordercolor white -border 4".
VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

Re: How to crop margin in imagemagick?

Post by VanGog »

Bingo!

Code: Select all

for /r %%F in (*.jpg) do ^
convert -density 400 -quality 34%% -trim ^
-gravity east -background white -splice 5x0 ^
"%%F" "%%~dF%%~pF%%~nF.pg"
VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

Re: How to crop margin in imagemagick?

Post by VanGog »

Well I will better move this post to new thread.
Post Reply