Code Monkey home page Code Monkey logo

crop-pages's Introduction

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.

crop-pages's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.