Пример #1
0
def subcommand_samples(input_file, state):
    """Generate a list of example files with different threshold values.

    :param input_file: The input file.
    :type input_file: jfscripts._utils.FilePath
    :param state: The state object.
    :type state: jfscripts.pdf_compress.State

    :return: None
    """

    args = state.args

    def fix_output_path(output_file):
        output_file = str(output_file).replace('_-000', '')
        return FilePath(output_file, absolute=True)

    if state.input_is_pdf:
        page_count = do_pdfinfo_page_count(input_file)
        page_number = random.randint(1, page_count)
        print('Used page number {} of {} pages to generate a series of images '
              'with different threshold values.'.format(
                  page_number, page_count))
        do_pdfimages(input_file, state, page_number)
        images = collect_images(state)
        input_file = FilePath(images[0], absolute=True)

    if args.threshold:
        for threshold in range(40, 100, 5):
            appendix = '_threshold-{}'.format(threshold)
            output_file = input_file.new(extension='tiff',
                                         append=appendix,
                                         del_substring=tmp_identifier)
            output_file = str(output_file).replace('_-000', '')
            do_magick_convert(input_file,
                              fix_output_path(output_file),
                              threshold='{}%'.format(threshold))

    if args.quality:
        for quality in range(40, 100, 5):
            appendix = '_quality-{}'.format(quality)
            output_file = input_file.new(extension='pdf',
                                         append=appendix,
                                         del_substring=tmp_identifier)
            do_magick_convert(input_file,
                              fix_output_path(output_file),
                              color=True,
                              quality=quality)

    if args.blur:
        for blur in (1, 2, 3, 4, 5):
            appendix = '_blur-{}'.format(blur)
            output_file = input_file.new(extension='pdf',
                                         append=appendix,
                                         del_substring=tmp_identifier)
            do_magick_convert(input_file,
                              fix_output_path(output_file),
                              color=True,
                              blur=blur,
                              quality=100)
Пример #2
0
 def assertAssemble(self, kwargs, called_with):
     with mock.patch('jfscripts.image_into_pdf.run.run') as run:
         # m = main
         # i = insert
         replace.assemble_pdf(FilePath('m.pdf'), FilePath('i.pdf'),
                              **kwargs)
         run.assert_called_with(['pdftk'] + called_with +
                                ['output', 'm_joined.pdf'])
 def test_extraction(self):
     pdf = FilePath(tmp_pdf)
     subprocess.check_output(['extract-pdftext.py', str(pdf)])
     txt = pdf.new(extension='txt')
     self.assertTrue(os.path.exists(str(txt)))
     self.assertTrue('## Seite' in open(str(txt)).read())
     self.assertTrue('Andrew Lloyd Webber' in open(str(txt)).read())
     self.assertTrue('-' *
                     extract_pdftext.line_length in open(str(txt)).read())
Пример #4
0
    def __init__(self, args):
        self.args = args
        """argparse arguments"""

        self.cwd = os.getcwd()
        """The current working directory"""

        self.input_files = []
        """A list of all input files."""
        if isinstance(self.args.input_files, str):
            self.input_files = [self.args.input_files]
        else:
            self.input_files = list_files.list_files(self.args.input_files)

        self.common_path = \
            list_files.common_path(self.input_files)
        """The common path prefix of all input files."""

        if self.common_path == '':
            self.common_path = self.cwd
        self.first_input_file = FilePath(self.input_files[0], absolute=True)
        """The first input file."""

        self.input_is_pdf = False
        """Boolean that indicates if the first file is a pdf."""

        if self.first_input_file.extension.lower() == 'pdf':
            self.input_is_pdf = True
Пример #5
0
def convert_image_to_pdf_page(image, image_width, pdf_width, pdf_density_x):
    print('image_width {} pdf_width {} pdf_density_x {}'.format(
        image_width, pdf_width, pdf_density_x))

    # pdf_density_x    x
    # -------------  = -----------
    # pdf_width        image_width

    # x = pdf_density_x / pdf_width * image_width

    # image_width: 1024
    # pdf_width: 542
    # pdf_density_x: 72

    # 72 / 542 * 1024 = 136,0295

    density = pdf_density_x / pdf_width * image_width

    message = 'Generate from the image file “{}” a temporary pdf file with ' \
              'the density of “{}”'
    print(message.format(image, density))
    tmp_pdf = os.path.join(tmp_dir, 'tmp.pdf')
    # '-compress', 'JPEG',
    # '-quality', '8',
    cmd_args = [
        'convert',
        str(image), '-units', 'PixelsPerInch', '-density',
        str(int(density)), tmp_pdf
    ]
    run.run(cmd_args)
    return FilePath(tmp_pdf)
Пример #6
0
def convert_file_paths(files):
    """Convert a list of file paths in a list of
    :class:`jfscripts._utils.FilePath` objects.

    :param list files: A list of file paths

    :return: a list of  :class:`jfscripts._utils.FilePath` objects.
    """
    out = []
    for f in files:
        out.append(FilePath(f, absolute=True))
    return out
Пример #7
0
def main():
    args = get_parser().parse_args()

    run.setup(verbose=args.verbose, colorize=args.colorize)

    check_dependencies(*dependencies)

    pdf = FilePath(args.file, absolute=True)
    txt_path = pdf.new(extension='txt')
    txt_file = Txt(txt_path)

    page_count = get_page_count(pdf)

    txt_file.add_line('# ' + pdf.basename)

    for i in range(1, page_count + 1):
        txt_file.add_line('')
        txt_file.add_line('-' * line_length)
        txt_file.add_line('')
        txt_file.add_line('## Seite ' + str(i))
        txt_file.add_line('')
        get_text_per_page(pdf, i, txt_file)
Пример #8
0
 def test_attribute_filename(self):
     file_path = FilePath('test.jpg')
     self.assertEqual(file_path.filename, 'test.jpg')
Пример #9
0
 def fix_output_path(output_file):
     output_file = str(output_file).replace('_-000', '')
     return FilePath(output_file, absolute=True)
Пример #10
0
 def test_attribute_base(self):
     file_path = FilePath('test.jpg', absolute=True)
     self.assertTrue(file_path.base.endswith('/test'))
Пример #11
0
def main():
    args = get_parser().parse_args()

    run.setup(verbose=args.verbose, colorize=args.colorize)

    check_dependencies(*dependencies)

    main_pdf = FilePath(args.pdf, absolute=True)
    image = FilePath(args.image, absolute=True)
    if hasattr(args, 'number'):
        number = args.number

    if image.extension == 'pdf':
        insert_pdf = image
    else:
        identify_pdf = do_pdftk_cat_first_page(main_pdf)
        pdf_dimensions = do_magick_identify_dimensions(identify_pdf)
        image_dimensions = do_magick_identify_dimensions(image)
        insert_pdf = convert_image_to_pdf_page(
            image,
            image_dimensions['width'],
            pdf_dimensions['width'],
            pdf_dimensions['x'],
        )

    info = get_pdf_info(main_pdf)

    if args.subcmd_args in ['add', 'ad', 'a']:

        if args.after:
            number = int(args.after[0])
            position = 'after'
        elif args.before:
            number = int(args.before[0])
            position = 'before'
        elif args.first:
            number = 1
            position = 'before'
        elif args.last:
            number = info['page_count']
            position = 'after'
        else:
            number = info['page_count']
            position = 'after'

        joined_pdf = assemble_pdf(main_pdf,
                                  insert_pdf,
                                  info['page_count'],
                                  number,
                                  mode='add',
                                  position=position)
        message = 'Successfully added the image “{}” {} page {} of the PDF ' \
                  'file “{}”. Result: “{}”'
        print(message.format(image, position, number, main_pdf, joined_pdf))

    elif args.subcmd_args in ['convert', 'cv', 'c']:
        if image.extension == 'pdf':
            raise ('Specify an image file, not a PDF file.')
        result_pdf = main_pdf.new(append='_insert')
        os.rename(str(insert_pdf), str(result_pdf))
        message = 'Successfully converted the image “{}” to the PDF file ' \
                  '“{}” using the dimensions of the PDF “{}”.'
        print(message.format(image, result_pdf, main_pdf))

    elif args.subcmd_args in ['replace', 're', 'r']:
        joined_pdf = assemble_pdf(main_pdf,
                                  insert_pdf,
                                  info['page_count'],
                                  args.number,
                                  mode='replace')
        message = 'Successfully replaced page {} of the PDF file “{}” with ' \
                  'the image “{}”. Result: “{}”'
        print(message.format(number, main_pdf, image, joined_pdf))
Пример #12
0
 def test_attribute_basename(self):
     file_path = FilePath('test.jpg')
     self.assertEqual(file_path.basename, 'test')
     file_path = FilePath('test.jpeg')
     self.assertEqual(file_path.basename, 'test')
Пример #13
0
 def test_convert(self):
     subprocess.run(
         ['image-into-pdf.py', 'convert', self.tmp_png, self.tmp_pdf])
     self.assertExists(str(FilePath(self.tmp_pdf).new(append='_insert')))
Пример #14
0
 def test_class_magic_method_eq_equal(self):
     a = FilePath('test.jpg')
     b = FilePath('test.jpg')
     self.assertTrue(a == b)
Пример #15
0
 def test_class_magic_method_eq_not_equal(self):
     a = FilePath('test1.jpg')
     b = FilePath('test2.jpg')
     self.assertFalse(a == b)
Пример #16
0
 def test_method_new(self):
     path = FilePath('test.jpg')
     self.assertEqual(str(path.new()), 'test.jpg')
     self.assertEqual(str(path.new(extension='png')), 'test.png')
     self.assertEqual(str(path.new(append='123')), 'test123.jpg')
     self.assertEqual(str(path.new(del_substring='est')), 't.jpg')
Пример #17
0
 def test_class_magic_method(self):
     file_path = FilePath('test.jpg')
     self.assertEqual(str(file_path), 'test.jpg')
Пример #18
0
 def test_class_argument(self):
     file_path = FilePath('test.jpg', absolute=True)
     self.assertEqual(str(file_path), os.path.abspath('test.jpg'))
Пример #19
0
 def test_attribute_extension(self):
     file_path = FilePath('test.jpg')
     self.assertEqual(file_path.extension, 'jpg')
Пример #20
0
 def assertExistsJoinedPdf(self):
     self.assertExists(str(FilePath(self.tmp_pdf).new(append='_joined')))