Skip to content
This repository has been archived by the owner on Nov 10, 2022. It is now read-only.
/ ndl-crop Public archive

Script for cropping photos from the NDL.

Notifications You must be signed in to change notification settings

polm/ndl-crop

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Update 2022-11-10:

This is a script I wrote on a weekend once years ago. It would be great if it helped you somehow, but there's probably better ways to do this. I don't provide support for this script and can't help you with your project.

ndl-crop

This is a script to crop scans of old photobooks from the NDL like those linked here. It's written for Python 3 and uses OpenCV for the heavy lifting.

Examples

Uncropped picture on the left, post auto-crop on the right.

Example 1

Example 2

Example 3

Example 4

Usage

Check out this repository, install the requirements, and feed it an image. It's recommended you use a virtualenv.

git clone https://github.com/polm/ndl-crop
cd ndl-crop
virtualenv env
source env/bin/activate
pip install -r requirements.txt
wget -O test.jpg 'http://dl.ndl.go.jp/view/jpegOutput?itemId=info%3Andljp%2Fpid%2F764232&contentNo=7&outputScale=4'
./ndl-crop.py test.jpg
# output is at test.cropped.jpg

If you have a folder full of images you can loop over it in bash.

for ii in raw-images/*; do
    ./ndl-crop.py $ii || true # sometimes it might fail
done

How it Works

Process GIF

  1. Load the image
  2. Threshold the image
  3. Adjust the threshold until a certain percent is black
  4. Generate contours
  5. Remove contours that are too large, small, near the edges, etc.
  6. Use the remaining contours to generate a bounding box for the crop

Limitations

This is designed to handle scans of single photos in albums and may not work well in the following cases.

  • spreads
  • single lines of text
  • multiple images on one page
  • scans with calibration scales in the margins
  • scans where the boundary between the photo and the paper is unclear
  • album covers
  • any image more than slightly rotated

Making a Process GIF

Use the -r flag to enable output and make a gif from that using ImageMagick.

./ndl-crop.py input.jpg
mogrify -resize 600x1000 frames/*
convert -delay 30 -loop 0 frames/* process.gif

License

WTFPL, do as you please.

About

Script for cropping photos from the NDL.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages