Skip to content
This repository has been archived by the owner on May 6, 2020. It is now read-only.

Added flag for quality #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions pdf-redact-tools
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ class PDFRedactTools(object):
self.pdf_filename = None
self.pages_dirname = None

# self.quality = '100'
# if quality:
# try:
# int_quality = int(quality)
# self.quality = quality
# except ValueError:
# print('Incorrect quality value given. Default \'100\' used.')

def set_pdf_filename(self, pdf_filename):
self.pdf_filename = os.path.abspath(pdf_filename)
self.output_filename = self.pdf_filename.replace('.pdf', '-final.pdf')
Expand All @@ -37,7 +45,7 @@ class PDFRedactTools(object):

self.transparent_filename = os.path.join(self.pages_dirname, 'page-transparent.png')

def explode(self):
def explode(self, quality='100'):
if not self.pdf_filename:
print 'Error: you must call set_pdf_filename before calling explode'
return False
Expand All @@ -54,7 +62,7 @@ class PDFRedactTools(object):
subprocess.call([ 'convert',
'-density', '128',
self.pdf_filename,
'-quality', '100',
'-quality', quality,
'-sharpen', '0x1.0',
self.transparent_filename])

Expand Down Expand Up @@ -140,6 +148,10 @@ def parse_arguments():
metavar='filename', dest='sanitize_filename',
type=lambda s:require_pdf(s),
help='Sanitize a PDF')
parser.add_argument('-q', '--quality',
metavar='quality', dest='png_quality',
type=int, default=100,
help='Quality for the exploded PNGs')
args = parser.parse_args()
return args

Expand All @@ -156,14 +168,15 @@ def main():
explode_filename = args.explode_filename
merge_filename = args.merge_filename
sanitize_filename = args.sanitize_filename
png_quality = str(args.png_quality)

pdfrt = PDFRedactTools()

# explode
if explode_filename:
if valid_pdf(explode_filename):
pdfrt.set_pdf_filename(explode_filename)
if pdfrt.explode():
if pdfrt.explode(png_quality):
print 'All done, now go edit PNGs in {} to redact and then run: pdf-redact-tools -m {}'.format(pdfrt.pages_dirname, pdfrt.pdf_filename)
else:
print explode_filename,' does not appear to be a PDF file, will not process'
Expand Down