Page 1 of 1

[Solved] Autocrop photographed Postcards (black border)

Posted: 2016-12-21T02:29:42-07:00
by bpk_os
Hi!

I’ve got a bunch (>1000) Canon RAW Files (*.CR2) of photograped postcards. The Postcards are all photograped in the same fashion and not 100% straight and have a bigger or smaller black border around them. Here is an sample file: https://cloudup.com/cuU67sTBe5P

My workflow now looks like this: Import RAW Files into Lightroom, crop & roate the image and apply minor enhancements, then export to tiff.

I’ve tried to use ImageMagick to automatically identify the size of the black border and crop the image to get rid of it, with mixed success.

I’ve been using Fred’s unrotate (http://www.fmwconcepts.com/imagemagick/ ... /index.php) and autotrim (http://www.fmwconcepts.com/imagemagick/autotrim/) scripts after I converted from RAW to tiff because I figured that IM can’t write or alter RAW files. So, using

Code: Select all

convert rawfile.CR2 tifffile.tif
and then

Code: Select all

unrotate -f 5 tifffile.tif unrotatedtiff.tiff
does not always work the way I want. Also, the TIFFs are about five times the size of the RAWs which I understand is normal.

How would you tackle this particular problem? I was thinking that there has to be a simple, automated solution for something like this, is there not?

Thanks for your help and consideration, I’ve been learning quite a lot about IM as a frequent reader of this forum.

---

I’m using IM on a MBPr 10.11

Code: Select all

identify -version
Version: ImageMagick 6.9.7-0 Q16 x86_64 2016-12-18
http://www.imagemagick.org
Delegates (built-in): bzlib freetype jng jpeg ltdl lzma png tiff xml zlib
That’s info on the RAW files:

Code: Select all

identify os_ub_0004379.CR2
os_ub_0004379.CR2=>/var/tmp/magick-13136Hhig0CYSQFOH.png CR2 5496x3670 5496x3670+0+0 16-bit sRGB 105.8MB 0.010u 0:00.009

Re: Autocrop photographed Postcards (black border)

Posted: 2016-12-21T11:09:01-07:00
by fmw42
My unrotate script probably does not work well for very small angles. Use -deskew for angles less than about 5 deg.

But your image has a white border around the outside of about 1-2 pixels. That is causing problems. Also you can get smaller file sizes for your tif by using lossless compression such as lzw.

The following works for me on your one example.

Code: Select all

convert os_ub_0004372_r.CR2 -compress lzw os_ub_0004372_r.tif

convert os_ub_0004372_r.tif -deskew 40% -shave 5x5 -fuzz 20% -trim +repage -compress lzw result.tif

Re: Autocrop photographed Postcards (black border)

Posted: 2017-01-10T03:12:54-07:00
by bpk_os
Hi Fred, thank you so much for your answer! Unfortunately the code you posted doesn’t work at all for me, neither the compression (tiffs are still way to large, sometimes they don’t even show a picture anymore?) nor the deskew/crop part. No error message at all. So I’m kind of stuck with this…

Also I’m not shure why you do see 1-2 pixels of a white border around the images, can’t reproduce that?

If you wouldn’t mind here is a dropbox folder with six example Images, any further help would be much appreciated!

https://www.dropbox.com/sh/zxpqt6ldn2i0 ... yryLa?dl=0

Re: Autocrop photographed Postcards (black border)

Posted: 2017-01-10T08:01:08-07:00
by bpk_os
I got some mixed results (depending on the image and the according fuzz value) when I
  • convertet the image from RAW to png
  • deskewed the png
  • shaved the border by 50px
  • floodfilled the output black
  • trimmed the image
for example:

Code: Select all

convert IMG_8175.CR2 image.png && convert image.png -deskew 40% +repage image_deskew.png && convert image_deskew.png -shave 50x50 image_shave.png && convert image_shave.png -fuzz 10% -fill black -draw 'color 0,0 floodfill' image_fill.png && convert image_fill.png -fuzz 50% -trim +repage image_trim.png
One thing that I assume makes removing the border so complicated are a lot of little dust particles in the black border.

The following image shows a zoomed-in comparison of a picture floodfilled (left) and original (right). The dust particle is clearly visible there.

Image

Re: Autocrop photographed Postcards (black border)

Posted: 2017-01-10T11:27:48-07:00
by fmw42
You can threshold your image so that the outside is black and the inside is white. There may be some black inside but do not be concerned. Then use -connected components to remove the noise areas and find the largest white region's bounding box. Then crop the thresholded image and deskew or unrotate it. Then use the same crop and rotate on your original image. See http://magick.imagemagick.org/script/co ... onents.php.

If your image has orientation information in the EXIF meta data, then you can use -auto-orient to convert between landscape and portrait by the proper rotation. See http://www.imagemagick.org/script/comma ... uto-orient

Re: Autocrop photographed Postcards (black border)

Posted: 2017-01-12T06:44:16-07:00
by bpk_os
How would you automate this for a whole folder of images?

I was able to identify the image’s bounding box with this command:

Code: Select all

convert in.png -threshold 10% -define connected-components:area-threshold=5510 -define connected-components:verbose=true -connected-components 4 -gamma 1.5 -auto-level -depth 8 out.png
Output is as follows:

Code: Select all

Objects (id: bounding-box centroid area mean-color):
  168: 4668x2939+465+367 2792.9,1832.9 13463240 srgb(100%,100%,100%)
  0: 5496x3670+0+0 2656.4,1837.7 6707080 srgb(0,0,0)
Here is the out.png: Image

Re: Autocrop photographed Postcards (black border)

Posted: 2017-01-27T02:24:34-07:00
by bpk_os
Turns out Photoshop does this exceptionally well, no problems whatsoever. Might not be free like IM but for us this is the way to go. Thanks for your help, I learned a lot about ImageMagick in the last weeks!