Skip to content

Commit

Permalink
Merge branch 'tcyrus-python-3'
Browse files Browse the repository at this point in the history
  • Loading branch information
mzucker committed Sep 29, 2016
2 parents becfa5c + 879dd66 commit 0701e13
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 21 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
noteshrink
==========

Convert scans of handwritten notes to beautiful, compact PDFs -- see full writeup at <https://mzucker.github.io/2016/09/20/noteshrink.html>
Convert scans of handwritten notes to beautiful, compact PDFs -- see full write-up at <https://mzucker.github.io/2016/09/20/noteshrink.html>

## Requirements

- python 2
- numpy
- scipy
- Python 2 or 3
- NumPy
- SciPy
- ImageMagick
- Image module from PIL or Pillow

## Usage
Expand Down
6 changes: 3 additions & 3 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ example_output/notesA.pdf: examples/notesA1.jpg examples/notesA2.jpg
cd example_output && \
../noteshrink.py -O -g -w -s 20 -v 30 -o notesA.pdf ../examples/notesA*.jpg

example_output/notesB.pdf: examples/notesB1.jpg examples/notesB2.jpg
example_output/notesB.pdf: examples/notesB1.jpg examples/notesB2.jpg
mkdir -p example_output && \
cd example_output && \
../noteshrink.py -O -g -w -s 4.5 -v 30 -o notesB.pdf ../examples/notesB*.jpg

example_output/tree.pdf: examples/tree.jpg
example_output/tree.pdf: examples/tree.jpg
mkdir -p example_output && \
cd example_output && \
../noteshrink.py -O -w -o tree.pdf ../examples/tree.jpg

example_output/graph-paper-ink-only.pdf: examples/graph-paper-ink-only.jpg
example_output/graph-paper-ink-only.pdf: examples/graph-paper-ink-only.jpg
mkdir -p example_output && \
cd example_output && \
../noteshrink.py -O -v 5 -o graph-paper-ink-only.pdf ../examples/graph-paper-ink-only.jpg
30 changes: 16 additions & 14 deletions noteshrink.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
# for some reason pylint complains about members being undefined :(
# pylint: disable=E1101

from __future__ import print_function

import sys
import os
import re
Expand Down Expand Up @@ -156,7 +158,7 @@ def postprocess(output_filename, options):
os.unlink(post_filename)

if not options.quiet:
print ' running "{}"...'.format(cmd),
print(' running "{}"...'.format(cmd), end=' ')
sys.stdout.flush()

try:
Expand All @@ -169,8 +171,8 @@ def postprocess(output_filename, options):
if result == 0:

if not options.quiet:
print '{:.1f}% reduction'.format(
100*(1.0-float(after)/before))
print('{:.1f}% reduction'.format(
100*(1.0-float(after)/before)))

return post_filename

Expand Down Expand Up @@ -321,7 +323,7 @@ def load(input_filename):
if pil_img.mode != 'RGB':
pil_img = pil_img.convert('RGB')

if pil_img.info.has_key('dpi'):
if 'dpi' in pil_img.info:
dpi = pil_img.info['dpi']
else:
dpi = (300, 300)
Expand Down Expand Up @@ -376,7 +378,7 @@ def get_palette(samples, options, return_mask=False, kmeans_iter=40):
'''

if not options.quiet:
print ' getting palette...'
print(' getting palette...')

bg_color = get_bg_color(samples, 6)

Expand Down Expand Up @@ -405,7 +407,7 @@ def apply_palette(img, palette, options):
'''

if not options.quiet:
print ' applying palette...'
print(' applying palette...')

bg_color = palette[0]

Expand Down Expand Up @@ -436,7 +438,7 @@ def save(output_filename, labels, palette, dpi, options):
'''

if not options.quiet:
print ' saving {}...'.format(output_filename)
print(' saving {}...'.format(output_filename))

if options.saturate:
palette = palette.astype(np.float32)
Expand Down Expand Up @@ -467,7 +469,7 @@ def get_global_palette(filenames, options):
all_samples = []

if not options.quiet:
print 'building global palette...'
print('building global palette...')

for input_filename in filenames:

Expand All @@ -476,7 +478,7 @@ def get_global_palette(filenames, options):
continue

if not options.quiet:
print ' processing {}...'.format(input_filename)
print(' processing {}...'.format(input_filename))

samples = sample_pixels(img, options)
input_filenames.append(input_filename)
Expand All @@ -492,7 +494,7 @@ def get_global_palette(filenames, options):
global_palette = get_palette(all_samples, options)

if not options.quiet:
print ' done\n'
print(' done\n')

return input_filenames, global_palette

Expand All @@ -511,7 +513,7 @@ def emit_pdf(outputs, options):
cmd = cmd.replace('%i', ' '.join(outputs))

if not options.quiet:
print 'running PDF command "{}"...'.format(cmd_print)
print('running PDF command "{}"...'.format(cmd_print))

try:
result = subprocess.call(shlex.split(cmd))
Expand All @@ -520,7 +522,7 @@ def emit_pdf(outputs, options):

if result == 0:
if not options.quiet:
print ' wrote', options.pdfname
print(' wrote', options.pdfname)
else:
sys.stderr.write('warning: PDF command failed\n')

Expand Down Expand Up @@ -551,7 +553,7 @@ def notescan_main(options):
options.basename, len(outputs))

if not options.quiet:
print 'opened', input_filename
print('opened', input_filename)

if not do_global:
samples = sample_pixels(img, options)
Expand All @@ -571,7 +573,7 @@ def notescan_main(options):
outputs.append(output_filename)

if not options.quiet:
print ' done\n'
print(' done\n')

emit_pdf(outputs, options)

Expand Down

0 comments on commit 0701e13

Please sign in to comment.