Giter Club home page Giter Club logo

data's Issues

fuzzy auto cropping / trimming aided by imagemagick

You can use imagemagick's fuzz and blur to automatically crop / trim jpegs scans using jpegtran:

#/usr/bin/env bash
for f in ./*.jpg; do
	fuzzFactor=5
	box=$(convert "$f" -virtual-pixel edge -blur 0x15 -fuzz "$fuzzFactor"% -trim -format '%[fx:w]x%[fx:h]+%[fx:page.x]+%[fx:page.y]' info:)
	if [ $(echo $box | awk -F'[x+]' '{print (0+$3)+(0+$4)}') != 0 ]; then
		jpegtran -copy all -crop "$box" -outfile ${f%.jpg}_out.jpg "$f"
	fi
done

That is, imagemagick is used not to modify the files but to output the cropbox which is then passed to jpegtran to do the actual (semi-lossless) cropping. Imagemagick and jpegtran conveniently use the same geometry format to specify crop boxes so we only need to check there's actually something to cut (using awk to add the crop values to see if they sum greater than 0) before passing the box to jpegtran.

It's a very useful trick when going through scanned material.

Anyhow, the the fuzzFactor depends on saturation and often needs tweaking. i.e. 5% is what I personally found safe with unprocessed scans off desktop scanners. But, if lucky, you might get away with as much as 15% if the contrast between the blacks and whites really stands out. However, it's rarely the case...

Incidentally, jpegtran crop helper is nice.

p.s. This method was derived from a lossy method found in the imagemagick v6 docs.

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.