3

I have ~200 scanned photos which I want to crop the white space out of. See example: enter image description here

Can someone provide me with the appropriate command line code to do this?... I have been trying to sort out the -trim and -fuzz options with no luck. NOT ALL images are same size (i.e. 4x6, 5x7, etc). All images were scanned/saved as jpg

Ideal scenario is a script where new trimmed photos are saved in one subdirectory.

Thanks in advance!

NRav
  • 407
  • 1
  • 6
  • 18

2 Answers2

2

I would suggest using -morphology to remove the scan artifacts, trim, and then capture the resulting paging.

PAGE_OFFSET=$(convert TrmkF.jpg -morphology Dilate:3 Diamond:3,5 -fuzz 10% -trim -format '%wx%h%O' info:-)

The $PAGE_OFFSET variable should now have the rough location of the scanned photo. We can apply that value with the -crop command.

convert TrmkF.jpg -crop $PAGE_OFFSET output.jpg

[![output][1]][1]

Edit

A (powershell) batch script may look as simple as...

Get-ChildItem "C:\path\to\photos" -Filter *.jpg | 
Foreach-Object {
    $pageOffset = magick $_.FullName -morphology Dilate:3 Diamond:3,5 -fuzz 10% -trim -format '%xx%h%O' info:- | Out-String
    $output = $_.FullName + ".output.jpg"
    magick $_.FullName -crop $pageOffset +repage $output
}

ymmv [1]: https://i.stack.imgur.com/u8bSs.png

Ryan
  • 4,602
  • 8
  • 37
  • 43
emcconville
  • 23,800
  • 4
  • 50
  • 66
  • Thanks! Now since I want to do this as a batch, how would I edit the code to not specify a specific file 'TrmkF.jpg' and have it the code simply crop each and save in a subdirectory with 'imageXXX.jpg' ... ? Thanks! – NRav Jun 20 '17 at 17:30
  • What is your ImageMagick version and platform? – fmw42 Jun 20 '17 at 17:35
  • Just use a `for loop` to iterate over files. In linux/osx use [bash script](https://unix.stackexchange.com/a/162589), or [powershell](https://stackoverflow.com/a/18848848/438117) in windows. – emcconville Jun 20 '17 at 17:40
  • I am using powershell in windows but very novice... how would I incorporate a 'for' loop ... ? I am good with python but this is my first crack at ImageMagick – NRav Jun 20 '17 at 17:42
  • @fmw42 I am using the latest version of ImageMagick – NRav Jun 20 '17 at 17:42
  • Is that ImageMagick 6 or ImageMagick 7? What platform? Please provide the full version number. You can do it with: convert -version or magick -version depending upon whether version 6 or 7, respectfully. Are all your images in one directory. – fmw42 Jun 20 '17 at 17:46
  • @fmw42 apologies, I did not know there was a difference in coding between 6 and 7. I am using Image Magick 7.0.6 with Q-16 bit – NRav Jun 20 '17 at 17:47
  • What platform? and what exact version of ImageMagick 7. try magick -version – fmw42 Jun 20 '17 at 17:48
  • Version: ImageMagick 7.0.6-0 Q16 x64 2017-06-11 – NRav Jun 20 '17 at 17:49
  • Unfortunately the batch script does not work... it splits a singular photo in to pieces 16 pieces instead of simply cropping the white space out – NRav Jun 20 '17 at 18:02
  • Windows 10 64 bit. – NRav Jun 20 '17 at 18:04
  • Try adding +repage after the crop; that is ... -crop $pageOffset +repage $output – fmw42 Jun 20 '17 at 18:05
  • For IM 7, the proper syntax is just magick, not magick convert. But it sounds like you are not getting the offsets part of your crop values, if you are getting lots of output images. Check the values that you are putting into the $pageOffset – fmw42 Jun 20 '17 at 18:07
  • convert: invalid argument for option '-crop': +repage @ error/convert.c/ConvertImageCommand/1206. – NRav Jun 20 '17 at 18:07
  • Try adding +repage after the crop; that is ... -crop $pageOffset +repage $output – fmw42 Jun 20 '17 at 18:08
  • See my comments above about using magick and not magick convert and also checking your $pageOffset values. – fmw42 Jun 20 '17 at 18:09
  • Yea, I have essentially copy/pasted the powershell batch code above and made the modifications as stated in comments... what values in pageOffset should I be editing? EDIT I am also still getting invalid argument for +repage – NRav Jun 20 '17 at 18:11
  • Not editing, but returning them so you can see what the values are. They should be of the form WxH+Xoff+Yoff. If they are missing +Xoff+Yoff, then that would account for getting multiple output images. Sorry I do not use Windows and do not know Powershell, but I understand the ImageMagick command. – fmw42 Jun 20 '17 at 18:12
  • Does Powershell allow you to echo or print to your CMD window the value of $pageOffset – fmw42 Jun 20 '17 at 18:14
  • 1
    Try replacing -format '%@' for -trim -format '%xx%h%g'. – fmw42 Jun 20 '17 at 18:17
  • I am trying to print $pageOffset, do not think it will work... Argh! – NRav Jun 20 '17 at 18:19
  • Try replacing -format '%@' for -trim -format '%xx%h%g'. I do not think '%xx%h%g' is correct. It should have been -trim -format "%wx%h%X%Y" – fmw42 Jun 20 '17 at 18:23
  • Alright so the image is not getting split into 8 or 16 parts, but now no cropping is being done... – NRav Jun 20 '17 at 18:26
  • Did you make my change above to use -format '%@' in place of -trim -format '%xx%h%g'? Or use -trim -format "%wx%h%X%Y". Do either work? If not then try one single image in your CMD window or Powershell as magick TrmkF.jpg -morphology Dilate:3 Diamond:3 -fuzz 20% -format "%@" info: Then copy the crop values that are returned and put them into magick TrmkF.jpg -crop cropvals +repage result.jpg Replace cropvals with the actual values you get, which should be of the form WxH+X+Y. If this works, then something is wrong with your Powershell script. – fmw42 Jun 20 '17 at 18:30
1

I've found that the above gives bad results, I think the formatting is different on MacOS or something so sharing the success story here. I have exactly this same issue - hundreds of scanned photos with some blotches in the white ruining the auto trim function.

I just modified parameters from the other individual's answer and got amazing results using this:

  1. cd into your folder of images
  2. mkdir ../done
  3. v
echo "$f";\
size=$(magick "$f" -bordercolor White -border 10x10 \
-morphology Dilate:5 Diamond:5,7 -fuzz 5% -trim \
-format "%wx%h%O" info:-); \
echo $size; \
magick "$f" -bordercolor White -border 10x10 -crop $size +repage "../done/$f"; done;
Deggen
  • 91
  • 1
  • 2