Skip to content

Commit

Permalink
Streamline names and options
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Morgan committed Jan 11, 2019
1 parent 40b2c35 commit d656979
Showing 1 changed file with 43 additions and 36 deletions.
79 changes: 43 additions & 36 deletions scanbro.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,16 @@ def command(self, input_file, output_file):
return cmd


class Convert(Processor):
"""Create a smaller file by compressing the image with ImageMagick."""
class ImageMagick(Processor):
"""
Compress the image with ImageMagick.
This is inappropriate for compressing PDFs, as the display size does not
remain constant and any text information in the PDF is discarded.
"""

binary = 'convert'
filetype = 'png'
profiles = Option('original', {
'original': [],
'scan': ["-normalize", "-level", "10%,90%", "-sharpen", "0x1"],
Expand Down Expand Up @@ -324,49 +330,47 @@ def command(self, input_file, output_file):
cmd.append(output_file)
return cmd

class Unpaper(Processor):
"""Create a smaller file by compressing the image with Unpaper."""

binary = 'unpaper'


class Ghostscript(Processor):
"""
Ghostscript preserves any text information that is in the PDF,
and is in this way useful to apply to a PDF that has been
augmented by tesseract.
Compress the PDF with Ghostscript.
Ghostscript preserves any text information that is in the PDF, and is in
this way useful to apply to a PDF that has been augmented by tesseract.
"""

binary = 'gs'
filetype = 'pdf'
profiles = Option('printer', {
'default': ['-dPDFSETTINGS=/default'],
'screen': ['-dPDFSETTINGS=/screen'],
'ebook': ['-dPDFSETTINGS=/ebook'],
'printer': ['-dPDFSETTINGS=/printer'],
'prepress': ['-dPDFSETTINGS=/prepress'],
})
modes = Option('color', {
'color': [],
'gray': ['-dConvertCMYKImagesToRGB=true', '-sProcessColorModel=DeviceGray', '-sColorConversionStrategy=Gray', '-dOverrideICC'],
})

def __init__(self, profile='printer'):
def __init__(self, profile='printer', mode='color'):
self.profile = profile
self.mode = mode

def command(self, input_file, output_file):
cmd = [
self.binary,
'-dNOPAUSE',
'-dSAFER',
'-dQUIET',
'-dBATCH',
'-sDEVICE=pdfwrite',
'-dCompatibilityLevel=1.4',
'-dCompressFonts=true',
'-dEmbedAllFonts=false',
'-dSubsetFonts=true',
'-dConvertCMYKImagesToRGB=true',
'-sProcessColorModel=DeviceGray',
'-sColorConversionStrategy=Gray',
'-dOverrideICC',
'-dNOPAUSE',
'-dSAFER',
'-dQUIET',
'-dBATCH',
f"-sOutputFile={output_file}",
]
cmd.extend(self.modes.args(self.mode))
cmd.extend(self.profiles.args(self.profile))
cmd.append(input_file)
return cmd
Expand Down Expand Up @@ -436,7 +440,7 @@ def make_scanner(args):
parser.add_argument(
'-r, --resolution',
dest='resolution',
default=None,
default='300',
choices=DEFAULT_SCANNER.resolutions.choices,
help='input scan resolution, in DPI',
)
Expand All @@ -452,15 +456,12 @@ def make_scanner(args):
def make_tesseract(args):
return Tesseract(args.language)
def make_convert(args):
return Convert(args.convert_profile, args.convert_quality)
def make_unpaper(args):
return Unpaper()
return ImageMagick(args.im_profile, args.convert_quality)
def make_ghostscript(args):
return Ghostscript(args.gs_profile)

FILTERS = {
'convert': make_convert,
'unpaper': make_unpaper,
'imagemagick': make_convert,
'tesseract': make_tesseract,
'ghostscript': make_ghostscript,
}
Expand All @@ -471,7 +472,7 @@ def make_ghostscript(args):
default=[],
action='append',
choices=FILTERS,
help='profiles for post-processing and output format',
help='filters for post-processing and output format',
)
parser.add_argument(
'-l, --language',
Expand All @@ -480,22 +481,28 @@ def make_ghostscript(args):
help='language the input should be interpreted in [tesseract]',
)
parser.add_argument(
'-q, --convert-quality',
dest='convert_quality',
choices=Convert.qualities.choices,
help='output quality of image [convert]',
'-i, --im-profile',
dest='im_profile',
choices=ImageMagick.profiles.choices,
help='output postprocessing profile of image [imagemagick]',
)
parser.add_argument(
'-t, --convert-profile',
dest='convert_profile',
choices=Convert.profiles.choices,
help='output postprocessing profile of image [convert]',
'-q, --im-quality',
dest='convert_quality',
choices=ImageMagick.qualities.choices,
help='output quality of image [imagemagick]',
)
parser.add_argument(
'-g, --gs-profile',
dest='gs_profile',
choices=Ghostscript.profiles.choices,
help='output compression profile of image [ghostscript]',
help='output compression profile of PDF [ghostscript]',
)
parser.add_argument(
'-e, --gs-mode',
dest='gs_mode',
choices=Ghostscript.modes.choices,
help='output color mode of PDF [ghostscript]',
)

# Run the program:
Expand Down

0 comments on commit d656979

Please sign in to comment.