Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support PDF #46

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
82 changes: 79 additions & 3 deletions noteshrink.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import re
import subprocess
import shlex
import fitz

from argparse import ArgumentParser

Expand Down Expand Up @@ -273,7 +274,10 @@ def get_argument_parser():
parser.add_argument('-c', dest='pdf_cmd', metavar="COMMAND",
default='convert %i %o',
help='PDF command (default "%(default)s")')


parser.add_argument('-f', dest='pymupdf_cmd',
action='store_true', default=False,
help='Save PDF as PyMuPDF')
return parser

######################################################################
Expand Down Expand Up @@ -500,10 +504,30 @@ def get_global_palette(filenames, options):

######################################################################

def pymupdf(outputs, options):
doc = fitz.open()
for i, f in enumerate(outputs):
img = fitz.open(f)
rect = img[0].rect
pdfbytes = img.convert_to_pdf()
img.close()
imgPDF = fitz.open("pdf", pdfbytes)
page = doc.new_page(width = rect.width,
height = rect.height)
page.show_pdf_page(rect, imgPDF, 0)

if len(options.filenames) == 1 and options.filenames[0].split(".")[-1] == "pdf":
ori = fitz.open(options.filenames[0])
doc.set_toc(ori.get_toc())
doc.save(options.pdfname)

def emit_pdf(outputs, options):

'''Runs the PDF conversion command to generate the PDF.'''

if options.pymupdf_cmd:
pymupdf(outputs, options)
return

cmd = options.pdf_cmd
cmd = cmd.replace('%o', options.pdfname)
if len(outputs) > 2:
Expand Down Expand Up @@ -579,9 +603,61 @@ def notescan_main(options):

######################################################################

def pdf_main(options):
# filenames = get_filenames(options)
pages = fitz.open(options.filenames[0])
outputs = []

do_global = options.global_palette and len(pages) > 1

if do_global:
filenames, palette = get_global_palette(filenames, options)

do_postprocess = bool(options.postprocess_cmd)

for each_page in pages:
pix = each_page.get_pixmap()
img = np.array(Image.frombytes("RGB", [pix.width, pix.height], pix.samples))
dpi = (pix.xres, pix.yres,)
if img is None:
continue

output_filename = '{}{:04d}.png'.format(
options.basename, len(outputs))

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

if not do_global:
samples = sample_pixels(img, options)
palette = get_palette(samples, options)

labels = apply_palette(img, palette, options)

save(output_filename, labels, palette, dpi, options)

if do_postprocess:
post_filename = postprocess(output_filename, options)
if post_filename:
output_filename = post_filename
else:
do_postprocess = False

outputs.append(output_filename)

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

emit_pdf(outputs, options)

def main():
'''Parse args and call notescan_main().'''
notescan_main(options=get_argument_parser().parse_args())
options = get_argument_parser().parse_args()
opt_filenames = options.filenames
if len(opt_filenames) == 1 and opt_filenames[0].split(".")[-1] == "pdf":
pdf_main(options=options)
else:
notescan_main(options=options)

if __name__ == '__main__':
main()
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ noteshrink
numpy>=1.10
scipy
Image
PyMuPDF