GitXplorerGitXplorer
p

ndl-crop

public
38 stars
4 forks
0 issues

Commits

List of commits on branch master.
Unverified
2bd2e9ce3bc5916fd0c9a24d4d8e787c835e1314

Add note about not updating this

ppolm committed 2 years ago
Unverified
c9c7e7201d87117f4e012e58f2cfd759e4f32a3f

Update deps

ppolm committed 3 years ago
Verified
1b5c371a704d8f9aec4c740eab2632c37c95c8d4

Merge pull request #7 from polm/dependabot/pip/numpy-1.22.0

ppolm committed 3 years ago
Verified
e21b43dfd073eb4fbf5c3e469c116142bc2694a9

Bump numpy from 1.20.3 to 1.22.0

ddependabot[bot] committed 3 years ago
Unverified
c80fb8120ebb6f9bdcc2224ab35627489e86a013

Update requirements

ppolm committed 4 years ago
Verified
b08040731c7a842ab4c7288e4cd22a431e25aa8f

Merge pull request #5 from polm/dependabot/pip/pillow-8.1.1

ppolm committed 4 years ago

README

The README file for this repository.

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.