auto-crop multiple old photographs from scanned 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
dsj

auto-crop multiple old photographs from scanned image?

Post by dsj »

Im attempting a project that might prove to be insane - scanning in all my old paper photographs.
you know the drill... theres typically 3 to 5 images that can fit on my flatbed scanner and they
vary in size and position in the scanned image.

Id like to be able to programatically auto-crop the individual images out of the larger scan image.

has anyone here done such a thing? can I use imagemagick APIs to do it? I can code but Im not
familiar with image processing techniques (yet). any help pointing me in the right direction would
be greatly appreciated - i really dont want to manually crop them one by one in photoshop! :-(

thanx!

.dsj.

ps - a secondary goal would be to auto-detect orientation and rotate them programatically if
its possible. being right 50%+ of the time is still a help. anyone ever done this? again, these are
scanned images so no EXIF info to help.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: auto-crop multiple old photographs from scanned image?

Post by anthony »

Post a URL to an example image in a new topic in the "User" Forum.

I am sure someone can figure out something with an example (preferable smallish) to work from.

Make it a 'difficult' image!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: auto-crop multiple old photographs from scanned image?

Post by fmw42 »

If the background color around the scanned image is relatively constant, you can try my two scripts autotrim and unrotate. You can get them at

http://www.fmwconcepts.com/imagemagick/index.html
dsj

Re: auto-crop multiple old photographs from scanned image?

Post by dsj »

anthony - posted to User forum as you suggested. thankyou.

fmw42 - thankyou very much for offering your help and time. autotrim returned basically the same
image. maybe I used it wrong?!?!? and I think unrotate will be invaluable once I get the individual
pix cropped out of the larger scanned image.

i noticed you also have a magicwand script. my first thought to solve this problem was I need
something like photoshop's magicwand with a 10 or so pixel threshold to identify the deadspace
between photos and then do an inverse select to select all the photos and crop from there. but
how to identify "deadspace"? could I use a scan without pictures as a sample of the lid of my
scanner to establish the base color to "find"? Im wondering if your magicwand will get me close if
I can use a color range instead of a position as position is different on each scan. once I have
seperate photos it straight forward to unrotate and run auto-levels on each pic.

again, I appreciate your time helping me with my problem. :-)

.dsj.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: auto-crop multiple old photographs from scanned image?

Post by fmw42 »

Sorry, I did not see your post until now. Perhaps you can post a link to an example image, if you have not already solved your problem.
dsj

Re: auto-crop multiple old photographs from scanned image?

Post by dsj »

I havent solved the problem yet. here's a sample image...
http://jonespix.com/imagemagick_example ... an0058.jpg

thanx. .dsj.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: auto-crop multiple old photographs from scanned image?

Post by fmw42 »

My autotrim and unrotate scripts only work on one image in a relatively constant background color.

Anthony has some script somewhere where he presumably extracted multiple pictures, but I think they had to be in one column. There was a post about that at one time some time ago. You might search the archives.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: auto-crop multiple old photographs from scanned image?

Post by anthony »

My method of extracting multiple images from a scan was based on fred's "multicrop" script. images could
be anywhere on the page.

But I added a some extra mask preparation code to make the separation of the images from non-images
better. I did submit that code to Fred, but I don't know if it is included or not.

=======8<--------
# # fill from the given coolrdinate point
# convert $tmp/IN.mpc -fuzz ${fuzzval}% -fill none \
# -draw "matte $coords floodfill" \
# -fill red +opaque none \
# $tmp/MASK.mpc

# add a border to set the background color (and/or flood fill from all edges)
# 'replace' is used instead of floodfill, with a morphology Open to avoid
# problems with 'thin decorative lines' that box or link images. It also
# tends to remove most 'text' and other 'too small' hits.

convert $tmp/IN.mpc -fuzz ${fuzzval}% -fill none \
-bordercolor $bgcolor -border 1x1 \
-draw "matte $coords replace" \
-shave 1x1 -fill red +opaque none \
-channel all -morphology Open Square:3 \
$tmp/MASK.mpc

=======8<--------

After the mask is prepared the script does a grid search of the mask looking for parts that are large enough to be an 'image'. When it finds one it crops the same area from the original image.

Works very well once you have the mask right.

This was for actual extraction of images from a magazine that liked adding the occasional thin decorative lines and boxes to a page.

After this I still needed to scan through the results as it could not separate two slightly overlapping images. But I did not really need to do much more.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: auto-crop multiple old photographs from scanned image?

Post by fmw42 »

Perhaps an example image will help. But look at my script, multicrop, at the link below.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: auto-crop multiple old photographs from scanned image?

Post by anthony »

I would like to say that while doing this is well and good. If you need to rotate the image, you will be distorting the image, and that means the image will become degraded to some extent.

The same happens for the most common type of distortion, resize. Though resizing smaller also averages more pixels, so while the resize degraded, it also in a sense sharpens and improves the image quality at the smaller scale.

So it is better if the images being scanned were not rotated, but properly aligned with the scanning matrix. That is why scanners have these edges, that you can push images against. It lines up the edge to match the scanning matrix. For multiple photos you can always add some extra 'edges' across the scanning area so you can place more images into a single scan. As a bonus the image location also becomes 'known', making auto-split and crop easier to handle.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Emerico
Posts: 1
Joined: 2013-07-25T07:39:36-07:00
Authentication code: 6789

Re: auto-crop multiple old photographs from scanned image?

Post by Emerico »

Well, most of the old photos are not half as crisp and clear as anything a cheap digital camera would produce nowadays. I've yet to see any distortion owing to rotation. There is an interpolation mechanism to make sure that high contrast parts don't look jagged after resize/rotate.
Lining up photos against the edges is a good idea though.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: auto-crop multiple old photographs from scanned image?

Post by anthony »

Not distortion per-say. It is more typically a general blurring coming from a step that is easily avoided.

More often than not, when you resize the image smaller, (for display or other use), any such extra blurring is of little consequence.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply